简体   繁体   English

如何使异步方法返回布尔值?

[英]How to make async methods that return a boolean?

I have the following method: 我有以下方法:

     public static boolean getIsUp()
     {
         String method = "isup.php";
         AsyncHttpResponseHandler response = new AsyncHttpResponseHandler(){

         @Override
             public void onSuccess(String content) {
            if(Integer.parseInt(content) == IS_UP_CONTENT)
                //code to return true
            else
                //code to return false
        }

     };
     get(method, null, response);
 }

How would you fill the //code to return true and //code to return false so that the method return the appropriate response? 您将如何填充//代码以返回true和//代码返回false,以便该方法返回适当的响应? Is that even possible? 那有可能吗?

I think you should not use an AsyncHttpResponseHandler inside a non-async method if you want to instantly return an result. 我认为如果要立即返回结果,则AsyncHttpResponseHandler非异步方法中使用AsyncHttpResponseHandler

What you can do is using an AsyncTask and the onPostExecute Callback 您可以使用AsyncTaskonPostExecute回调进行操作

This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. 此类允许执行后台操作并在UI线程上发布结果,而无需操纵线程和/或处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM