gpt4 book ai didi

Android线程完成回调

转载 作者:行者123 更新时间:2023-11-29 17:42:00 25 4
gpt4 key购买 nike

我想在收到服务器响应后执行 http post。如果服务器响应为 false,则 http post 将执行,否则不执行。我该怎么做。

我的安卓主要 Activity 代码:

if (Utility.isValidMobile(mobileNumber)) {
String isAvailable = userDelegate.checkUser(mobileNumber, context);


if (isAvailable.equals("false")) {
userDelegate.addUser(userMO, context);
Toast.makeText(getApplicationContext(), "Your mobile number is" + mobileNumber + "name is" + userName, Toast.LENGTH_LONG).show();
} else if (isAvailable.equals("true")) {
Toast.makeText(getApplicationContext(), "Your mobile number is already registerd", Toast.LENGTH_LONG).show();
}
}

当我点击注册按钮时,上面的代码将被执行

我的 Userdelegate 类代码:

public void addUser(final UserMO userMo, final Context context) {

final String jsonStringObject = gson.toJson(userMo);

Thread t = new Thread() {
public void run() {
Looper.prepare(); // for the child Thread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
// Limit
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("userBO", jsonStringObject));
HttpPost post = new HttpPost("http://192.168.1.101:8080/warname/user/addUser");
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
Toast.makeText(context, "Your user id " + rd.readLine(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
Looper.loop(); // Loop in the message queue
}
};
t.start();

}

public void getMatchingExistingUserList(final String mobile_number, final Context context) {

final String jsonStringObject = gson.toJson(mobile_number);

Thread t = new Thread() {
public void run() {
Looper.prepare(); // for the child Thread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
// Limit
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("userBO", jsonStringObject));
HttpPost post = new HttpPost("http://192.168.1.101:8080/warname/user/addUser");
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
final String responseString = rd.readLine();
} catch (Exception e) {
e.printStackTrace();
}
Looper.loop(); // Loop in the message queue
}
};
t.start();
}

public String checkUser(final String mobile_number, final Context context) {

final StringBuilder isAvailable = new StringBuilder();

Thread t = new Thread() {
@Override
public void run() {
Looper.prepare(); // for the child Thread
try {
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("MobileNumber", gson.toJson(mobile_number)));
HttpPost post = new HttpPost("http://192.168.1.101:8080/warname/user/checkUserMobileNumber");
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
isAvailable.append(rd.readLine());
} catch (Exception e) {
e.printStackTrace();
}
Looper.loop(); // Loop in the message queue
}
};
t.start();
return isAvailable.toString();
}

问题是我得到的响应是错误的,但 if 条件不起作用。如何解决这个问题。

改变后:

if (Utility.isValidMobile(mobileNumber)) {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... arg0) {
return userDelegate.checkUser(mobileNumber, context);
}

@Override
protected void onPostExecute(String isAvailable) {
Toast.makeText(getApplicationContext(), isAvailable, Toast.LENGTH_LONG).show();
if (isAvailable.equals("false")) {
Toast.makeText(getApplicationContext(), "Your mobile number is" + mobileNumber + "name is" + userName, Toast.LENGTH_LONG).show();
userDelegate.addUser(userMO, context);
} else if (isAvailable.equals("true")) {
Toast.makeText(getApplicationContext(), "Your mobile number is already registerd", Toast.LENGTH_LONG).show();
}
}
}.execute(null, null, null);
}

if 条件不工作?

最佳答案

If server response is false the http post will execute else not execute. How can i do for this

出现问题是因为您在 checkUser 中使用线程和 addUser .线程的执行没有停止当前线程的执行。

例如,当checkUser然后从主线程调用方法 final StringBuilder isAvailable = new StringBuilder();在主线程和线程上执行 t正在单独执行。所以系统将控制返回到下一行 return isAvailable.toString();无需等待线程执行完成意味着 checkUser方法总是返回 nullempty字符串。

同样适用于 addUser方法。

根据checkUser的结果做任务方法响应使用AsyncTask类(class)。

关于Android线程完成回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28359108/

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