【问题标题】:Returning Boolean value from Asynctask in Android从 Android 中的 Asynctask 返回布尔值
【发布时间】:2014-04-07 16:17:32
【问题描述】:

我是安卓新手。我想要做的只是将一个 URL 传递给 Asynctask,然后检查是否返回状态代码 SC_OK。如果返回状态码 SC_OK,则该方法应返回 true,如果返回任何其他状态码,则该方法应返回 false。

class checking extends AsyncTask<String, Void, Boolean> {

    @Override
    protected Boolean doInBackground(String... params) {
        // TODO Auto-generated method stub
        Boolean a;
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(l1);      
        HttpResponse response;
        try {
            response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {                                   
                Toast.makeText(getApplicationContext(),"No", Toast.LENGTH_LONG).show();
                a= false;
            }
            else {
                Toast.makeText(getApplicationContext(),"Yes", Toast.LENGTH_LONG).show();
                a= true; 
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            return false;
        }              
    }

    @Override
    protected void onPostExecute(Boolean result) {

    }
}

【问题讨论】:

  • 有什么问题?
  • a finally 块总是被调用,所以你总是会返回 false。也许您想删除此块并添加 return a;最后?
  • 没有工作,但现在工作正常。

标签: java android android-asynctask status


【解决方案1】:

把整个东西改成(盲编码):

class checking extends AsyncTask<String, Void, Boolean> {
    @Override
    protected Boolean doInBackground(String... params) {
        // TODO Auto-generated method stub
        Boolean a;
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(l1);
        HttpResponse response;
        try {
            response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                a = false;
            } else {
                a = true;
            }
            return a;
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
    @Override
    protected void onPostExecute(Boolean result) {
        Toast.makeText(getApplicationContext(), (result ? "Yes" : "No"), Toast.LENGTH_LONG).show();
    }
}

【讨论】:

    【解决方案2】:

    您正在将本地布尔值 a 设置为 true,但从不返回该值。该方法如其所写,将始终返回false

    【讨论】:

      猜你喜欢
      • 2013-05-21
      • 1970-01-01
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      相关资源
      最近更新 更多