Aar*_*ron 0 android countdownlatch android-asynctask
我在我的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;
}
Run Code Online (Sandbox Code Playgroud)
我也知道asyncTask.execute(args).get()可能导致异常,所以我使用一个监听器来更新调用者的值,我在我的AsyncTask的onPostExecute上调用setResponse.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
现在这是我的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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的日志的最后一行是
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..
Run Code Online (Sandbox Code Playgroud)
NetworkOnMainException提醒我们不要阻止主UI线程.但是,您仍然使用a CountDownLatch等待响应来阻止它.由于onPostExecute()在主线程上运行,因此您已将其阻止执行.在等待网络连接完成时,您无法阻止主线程.相反,允许executeRequest()返回以便UI可以适当地响应.
| 归档时间: |
|
| 查看次数: |
348 次 |
| 最近记录: |