Android : Post request failed in android application

on Friday, September 19, 2014


I’m trying to do a login system for my application. The problem that when I add a Bundle to set information for my fragment, the request respond show that the account doesn’t exist, on the other side the log display me the right account information, wath mean that the request it's well done, but i don't why the onPostExecute show me the false response. Here is the example



@Override
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("Error !");
builder.setMessage("The information entered is incorrect.\nPlease try again!")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();

}

}


My DoInBackground Function :



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();
JSONObject jObj = new JSONObject(result);

//Affichage de test
System.out.println(jObj);

if(jObj.isNull("false")){

//Créer un Bundle pour déposer les infos
infos = new Bundle();
infos.putString("ID",jObj.getString("ID"));
infos.putString("Name",jObj.getString("display_name"));
infos.putString("ImgUrl","http://unchained-network.com/uploads/profilpics/53f5c570b6ac2.png");


Fragment fg = getFragmentManager().findFragmentById(R.id.frgmnt);
//Charger les infos dans l'activité
fg.setArguments(infos);

Intent myIntent = new Intent(getActivity(), HomesActivity.class);
//Lançer l'activité
startActivityForResult(myIntent, 0);

}

}catch (Exception e){

return false;
}

return true;
}


The log response :



09-19 10:00:48.139 2326-2346/com.example.user.unchained I/System.out﹕ {"ID":"50","filename":"5379aae5ef60a.jpeg","display_name":"karim ennassiri"}

0 comments:

Post a Comment