gpt4 book ai didi

android - 发布数据 AsyncTask 不会执行 android

转载 作者:行者123 更新时间:2023-11-30 03:13:49 27 4
gpt4 key购买 nike

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

最佳答案

似乎这是在运行,问题出在 if(test.contains("\"success\":true\"")) - if 语句不成立所以我可以完成 Activity

关于android - 发布数据 AsyncTask 不会执行 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20482970/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com