我正在尝试使用HTTP请求连接到mysql数据库。我创建了一个扩展AsyncTask广告的类,负责发出http post请求和检索数据。这是我的代码:
public class AttemptLogin extends AsyncTask {
String mUserName;
String mPassword;
String un = "admin01";
String pass = "admin01";
Connection con;
Context context;
AlertDialog dialog ;
public AttemptLogin(Context context, String mUserName, String Password) {
this.context = context;
this.mUserName = mUserName;
this.mPassword = Password;
}
@Override
protected void onPreExecute(){
dialog = new AlertDialog.Builder(context).create();
dialog.setTitle("Login Status");
}
protected void onPostExecute(String s){
dialog.setMessage(s);
dialog.show();
}
@Override
protected Object doInBackground(Object[] objects) {
try{
Log.d(tag, "Debut do in background");
String constr = "http://000.000.0.00:3306/login.php";
URL url = new URL(constr);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setDoInput(true);
http.setDoOutput(true);
http.setUseCaches(false);
http.setRequestMethod("POST");
http.setRequestProperty("Connection", "close");
OutputStream ops = http.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ops,"UTF-8"));
String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(mUserName, "UTF-8") + " & " +
URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(mPassword, "UTF-8");
writer.write(data);
writer.flush();
writer.close();
ops.close();
String result = "";
String line = "";
InputStream ips = http.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ips,"ISO-8859-1"));
while ((line = reader.readLine()) != null){
result += line;
}
Log.d(tag, "Result = " + result);
reader.close();
ips.close();
http.disconnect();
onPostExecute(result);
return result;
}
catch (Exception e) {
Log.w("Error connection","" + e.getMessage());
e.printStackTrace();
}
return null;
}
}
当然,我放置的IP只是为了隐藏我的真实IP。无论如何,getInputStream()的行返回以下错误:意外的状态行:Y������
我已经尝试了很多事情,包括将“连接”属性设置为“关闭”,但它没有成功。
任何帮助都会受到赞赏,因为我已经被困了一段时间了。谢谢!
您似乎正在尝试向运行MySQL特定协议的端口发出HTTP请求。请求发生,但响应不是HTTP响应,因此是异常。