Android:AsyncTask没有达到onPostExecute

时间:2017-07-10 16:03:50

标签: android android-asynctask countdownlatch

我在我的Android库项目中使用AsyncTask来避免networkonmainthreadexception,现在我想做的是将值返回到我的变量并在执行下一行代码之前等待它。我使用CountDownLatch来做到这一点。

我执行请求并调用AsyncTask的方式如下所示。所以我使用CountDownLatch等待响应,然后返回我想要返回的值。

public MyObject getMyObject(){

    final String url = "http://[myip]:8080/retrieve";

    try{
        Response response = executeRequest(url);
        final MyObject myObject = om.readValue(response.body().string(),
                new TypeReference<MyObject>() {
                });
        return myObject;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;

}



private Response executeRequest(String url){

    try {
      //Simplified please just ignore this..
      GenericAsyncParams genericAsyncParams = createParams(......);

      final CountDownLatch countDownLatch = new CountDownLatch(1);
      ResponseListener responseListener = new ResponseListener(countDownLatch);
      MyAsyncTask myAsyncTask = new AsyncTask(responseListener);
      myAsyncTask.execute(genericAsyncParams);
      countDownLatch.await();

      //get the response from the listener class.
      Response response = responseListener.getResponse();
      return response;
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    return null;
  }

我也知道 asyncTask.execute(args).get()可能会导致异常,所以我用一个监听器来更新调用者的值,我在onPostExecute上调用setResponse的AsyncTask。

public class ResponseListener {

    private static final String TAG = ResponseListener.class.getName();

    private Response response;
    private CountDownLatch countDownLatch;

    public ResponseListener(CountDownLatch countDownLatch) {
        this.response = null;
        this.countDownLatch = countDownLatch;
    }

    public void setResponse(Response response){
        this.response = response;   
        Log.e(TAG, "Executing countdown..");
        countDownLatch.countDown();
    }

    public Response getResponse() {
        return response;
    }

    public CountDownLatch getCountDownLatch() {
        return countDownLatch;
    }
}

现在这是我的AsyncTask 注意:requestProcessor是okHttp的包装类。

public class MyAsynctask extends AsyncTask<GenericAsyncParams, Void, Response>{


    private static final String TAG = MyAsyncTask.class.getName();

    private ResponseListener listener;

    public MyAsynctask(ResponseListener listener) {
        this.listener = listener;
    }

    @Override
    protected Response doInBackground(GenericAsyncParams... genericAsyncParamses) {
        Response response = null;
        GenericAsyncParams genericAsyncParams = genericAsyncParamses[0];
        String url = genericAsyncParams.getUrl();
        String method = genericAsyncParams.getMethod();
        RequestProcessor requestProcessor = genericAsyncParams.getRequestProcessor();

        try {
            Request.Builder builder;
            if(method.equalsIgnoreCase("POST")){
                builder = new Request.Builder()
                        .post(genericAsyncParams.getRequestBody())
                        .url(url)
                        .headers(headers);
            } else  {
                builder = new Request.Builder()
                        .headers(headers)
                        .get()
                        .url(url);
            }
            final Request request = builder.build();
            response = requestProcessor.client().newCall(request).execute();
            Log.d(TAG, "returning okHttp response..");
            return response;
        } catch (Exception e){
            Log.e(TAG, String.format("ERROR.. %s", ExceptionUtils.getMessage(e)));
        }

        return response;
    }


    @Override
    protected void onPostExecute(Response response) {
        if(listener != null){
            Log.d(TAG, "Updating response : onPostExecute..");
            listener.setResponse(response);
        } else {
            super.onPostExecute(response);
        }
    }

}

我的日志的最后一行是

07-10 23: 47:07.008 28409-28530/com.aaron.android.android.lib D/com.aaron.android.android.lib.api.tasks.MyAsyncTask: returning okHttp response..

1 个答案:

答案 0 :(得分:5)

NetworkOnMainException提醒我们不要阻止主UI线程。但是,您仍然使用CountDownLatch等待响应来阻止它。由于onPostExecute()在主线程上运行,因此您已将其阻止执行。在等待网络连接完成时,您无法阻止主线程。相反,允许executeRequest()返回,以便UI可以做出适当的响应。