Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android线程已完成回调_Android_Performance_Android Intent_Android Activity_Android Asynctask - Fatal编程技术网

Android线程已完成回调

Android线程已完成回调,android,performance,android-intent,android-activity,android-asynctask,Android,Performance,Android Intent,Android Activity,Android Asynctask,我想在从服务器得到响应后执行http post。 如果服务器响应为false,则http post将执行,否则不会执行。 我该怎么办呢 我的android主活动代码: if (Utility.isValidMobile(mobileNumber)) { String isAvailable = userDelegate.checkUser(mobileNumber, context); if (isAvailable.equals("false

我想在从服务器得到响应后执行http post。 如果服务器响应为false,则http post将执行,否则不会执行。 我该怎么办呢

我的android主活动代码:

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();
}
public void addUser(final UserMO UserMO,final Context){
最后一个字符串jsonStringObject=gson.toJson(userMo);
线程t=新线程(){
公开募捐{
Looper.prepare();//用于子线程
HttpClient=new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),10000);//超时
//极限
试一试{
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“userBO”,jsonStringObject));
HttpPost=新的HttpPost(“http://192.168.1.101:8080/warname/user/addUser");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=client.execute(post);
BufferedReader rd=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
Toast.makeText(上下文,“您的用户id”+rd.readLine(),Toast.LENGTH_LONG.show();
}捕获(例外e){
e、 printStackTrace();
}
Looper.loop();//消息队列中的循环
}
};
t、 start();
}
public void getMatchingExistingUserList(最终字符串mobile\u编号,最终上下文){
最终字符串jsonStringObject=gson.toJson(手机号码);
线程t=新线程(){
公开募捐{
Looper.prepare();//用于子线程
HttpClient=new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),10000);//超时
//极限
试一试{
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“userBO”,jsonStringObject));
HttpPost=新的HttpPost(“http://192.168.1.101:8080/warname/user/addUser");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=client.execute(post);
BufferedReader rd=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
最终字符串responseString=rd.readLine();
}捕获(例外e){
e、 printStackTrace();
}
Looper.loop();//消息队列中的循环
}
};
t、 start();
}
公共字符串检查用户(最终字符串移动\u编号,最终上下文){
最终StringBuilder isAvailable=新StringBuilder();
线程t=新线程(){
@凌驾
公开募捐{
Looper.prepare();//用于子线程
试一试{
HttpClient=new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),10000);//超时
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“MobileNumber”,gson.toJson(手机号码));
HttpPost=新的HttpPost(“http://192.168.1.101:8080/warname/user/checkUserMobileNumber");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=client.execute(post);
BufferedReader rd=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
isAvailable.append(rd.readLine());
}捕获(例外e){
e、 printStackTrace();
}
Looper.loop();//消息队列中的循环
}
};
t、 start();
返回isAvailable.toString();
}
问题是我得到了错误的响应,但如果条件不工作。 如何解决这个问题

更改后:

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(Utility.isValidMobile(mobileNumber)){
新建异步任务(){
@凌驾
受保护的字符串doInBackground(无效…arg0){
返回userDelegate.checkUser(mobileNumber,context);
}
@凌驾
受保护的void onPostExecute(字符串可用){
Toast.makeText(getApplicationContext(),isAvailable,Toast.LENGTH_LONG).show();
if(isAvailable.equals(“false”)){
Toast.makeText(getApplicationContext(),“您的手机号码是”+mobileNumber+“姓名是”+userName,Toast.LENGTH_LONG).show();
addUser(userMO,上下文);
}else if(isAvailable.equals(“true”)){
Toast.makeText(getApplicationContext(),“您的手机号码已注册”,Toast.LENGTH\u LONG.show();
}
}
}.执行(空,空,空);
}
如果条件不起作用

如果服务器响应为false,则将执行http post,否则不会执行 执行。我该怎么办

出现问题的原因是您正在使用
checkUser
addUser
中的线程。线程的执行不停止当前线程的执行

例如,当从主线程调用
checkUser
方法时,则
final StringBuilder isAvailable=new StringBuilder()在主线程上执行,线程
t
分别在中执行。因此,系统将控件返回到下一行
return isAvailable.toString()
不等待线程执行完成意味着
checkUser
方法始终返回
null
em
class Listener{
    private Handler handler = new Handler();
    public void onUserAdded(){
         handler.post(new Runnable(){
               public void run(){
                     // Toast your thing
               }
         });
    }

    public void onUserChecked(final boolean available){
         handler.post(new Runnable(){
               public void run(){
                     if(available){
                           // Toast your thing
                     }else{
                          userDelegate.addUser(userMO, context);
                     }
               }
         });
    }
}