Reputation: 3912
I have this simple code where i do post to a server some data. As this is happening on click where i call the function that starts the asyncTask like this:
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
editProfile();
}
});
And here is my editProfile class:
private void editProfile() {
Log.v("--", "start");
final String newAddress = address.getText().toString();
final String newPhone = phone.getText().toString();
final String newEmail = email.getText().toString();
final String newStatus = status.getText().toString();
final String registerURL = "http://myurl/companyapp/"
+ "user.php?action=edit&user_id=" + userID + "&address="
+ newAddress + "&phone=" + newPhone + "&" + "email=" + newEmail
+ "&status=" + newStatus;
Log.v("--", registerURL);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(registerURL);
// httppost.setHeader("Accept", "application/json");
httppost.setHeader("Accept",
"application/x-www-form-urlencoded");
// httppost.setHeader("Content-type",
// "application/json");
httppost.setHeader("Content-Type",
"application/x-www-form-urlencoded");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
5);
nameValuePairs.add(new BasicNameValuePair("user_id", userID
+ ""));
nameValuePairs
.add(new BasicNameValuePair("address", newAddress));
nameValuePairs.add(new BasicNameValuePair("phone", newPhone));
nameValuePairs.add(new BasicNameValuePair("email", newEmail));
nameValuePairs
.add(new BasicNameValuePair("status", newStatus));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,
"UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
test = EntityUtils.toString(entity);
if (test.contains("\"success\":true\"")) {
Toast.makeText(EditProfile.this,
getString(R.string.profile_updated),
Toast.LENGTH_SHORT).show();
finish();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}.execute();
}
In logcat i can see that the function is called but the asyncTask wont start. Can anyone tell me why is this happening?
Upvotes: 1
Views: 248
Reputation: 3912
Seems that this was running, the problem was in the if(test.contains("\"success\":true\""))
- that if statement didn't go true so I can finish the activity
Upvotes: 1