Android : Post request not responding for an android app

on Friday, August 29, 2014


I'm trying to make a POST request function in android I’ve made all thing right but the request its display an error. This the exemple


This the doBackground function.



//doInBackground
private final String mEmail;
private final String mPassword;
String url = "index.php/services/UserLogin";

LoginTask(String email, String password) {
mEmail = email;
mPassword = password;
}

@Override
protected Boolean doInBackground(Void... params) {
// TODO: attempt authentication against a network service.

try {
// Simulate network access.
Thread.sleep(2000);
} catch (InterruptedException e) {
return false;
}

ArrayList<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", mEmail));
nameValuePairs.add(new BasicNameValuePair("password", mPassword));

//Prepare the Post query

try {
HttpClient clientHttp = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = clientHttp.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = reader.readLine();
sb.append(line + "\n");
is.close();

String result = sb.toString();
System.out.println(result);

}catch (Exception e){

return false;
}


// TODO: register the new account here.
return true;
}


and this is the doPost function :



//onPostExecute
protected void onPostExecute(final Boolean success) {
//LoginTask = null;
//showProgress(false);
if (success) {
finish();
} else {
//password.setError(getString(R.string.error_incorrect_password));
//password.requestFocus();

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Erreur !");
builder.setMessage("Les informations entrées sont incorrectes.\nVeuillez réessayer!")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}


So have you seen any error in my code thanks for the help !


0 comments:

Post a Comment