我有这个简单的代码,我会在服务器上发布一些数据。因为这发生在click,我调用启动asyncTask的函数,如下所示:
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
editProfile();
}
});
这是我的editProfile类:
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();
}
在logcat中我可以看到该函数被调用但asyncTask不会启动。谁能告诉我为什么会这样呢?
答案 0 :(得分:1)
似乎正在运行,问题出在if(test.contains("\"success\":true\""))
- if语句没有成功,所以我可以完成活动