我有这个简单的代码,我确实将一些数据发布到服务器。由于这是在单击时发生的,因此我调用了启动 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 不会启动。谁能告诉我为什么会这样?