I am trying to connect to a mysql database using an HTTP request. I have created a class which extends AsyncTask ad takes care of making the http post request and retrieving the data. Here's my code:
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;
}
}
Of course, the IP I've placed is just to hide my real IP. Anyway, the line at getInputStream() is returning the following error: Unexpected status line: Y������
I've tried many things incuding setting the "Connection" property to "close" but it didn't work out.
Any help would be appreciated as I've been stuck for some time now. Thank you!