1
내가 AsyncTask를 문제를 가지고, 그것은 UI를 차단하고 화면이AsyncTask를 블록 UI 스레드
를 동결 할이 내 코드
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
AdServerManager.getInstance().getOfferClickCount(MultiOfferDetailsActivity.this, mCurrentOffer.getId(), ClickType.LIKE, new ClickViewCountUIListener() {
@Override
public void onClickViewCountCompleted(IncrementClickResponse response, AppError error) {
if (response != null)
mOfferLikes.setText(getString(R.string.likes2) + " " + PhoneUtils.decimalFormat(MultiOfferDetailsActivity.this, response.getCount()));
else
mOfferLikes.setText("");
}
});
return null;
}
}.execute();
이며,이 서버 메소드의 코드
public void getOfferClickCount(final Context context, final int id, final ClickType clickType, final ClickViewCountUIListener uiListener) {
if (BasePhoneUtils.isNetworkAvailable(context) == false) {
uiListener.onClickViewCountCompleted(null, AppError.INTERNET_ERROR);
} else {
new AsyncTask<Void, Void, BaseResponse>() {
@Override
protected BaseResponse doInBackground(Void... sparams) {
mStartTime = System.nanoTime();
try {
String urlToCall = AdsConstants.ADS_SERVER_URL + AdsConstants.OFFER_GET_CLICK_COUNT;
String param = String.format(AdsConstants.OFFER_GET_CLICK_COUNT_PARAM_FORMAT, id, clickType.getValue());
Log.e("AdServerManager", urlToCall);
Log.e("AdServerManager", param);
BaseRequest request = new BaseRequest();
request.setURL(urlToCall);
request.setRequestType(RequestType.POST);
request.setStringParam(param);
mStopTime = System.nanoTime();
PerformanceManager.getInstance().showCalculatedTime("getOfferClickCount [doInBackground]", mStartTime, mStopTime);
return NetworkManager.execute(context, request, false, true);
} catch (Exception e) {
e.printStackTrace();
uiListener.onClickViewCountCompleted(null, AppError.DATA_ERROR);
return null;
}
}
protected void onPostExecute(BaseResponse result) {
mStartTime = System.nanoTime();
if (result != null && result.getResponse() != null) {
try {
uiListener.onClickViewCountCompleted(IncrementClickResponse.parseJSONObject(result.getResponse()), null);
} catch (JSONException e) {
e.printStackTrace();
uiListener.onClickViewCountCompleted(null, AppError.PARSING_ERROR);
} catch (ParseException e) {
e.printStackTrace();
uiListener.onClickViewCountCompleted(null, AppError.PARSING_ERROR);
}
} else {
uiListener.onClickViewCountCompleted(null, AppError.GENERAL_ERROR);
}
mStopTime = System.nanoTime();
PerformanceManager.getInstance().showCalculatedTime("getOfferClickCount [onPostExecute]", mStartTime, mStopTime);
}
}.execute();
}
}
'IncrementClickResponse.parseJSONObject (result.getResponse())'를'doInBackground()로 이동하면 가능한 빠른 수정으로 THREAD_POOL_EXECUTOR를 사용하여 작업을 시도해 볼 수 있습니다.)', 도움이되는지 확인하십시오. – CommonsWare
아니요 작동하지 않습니다 –
동시에 여러 개의 AsyncTasks가 활성화되어있는 것처럼 보입니다. Honeycomb에서 AsyncTask의 기본 실행 프로그램은 SERIAL_EXECUTOR이므로 언제든지 하나의 AsyncTask 만 실행할 수 있으며 교착 상태가 발생할 수 있습니다. 가능한 빠른 수정으로 THREAD_POOL_EXECUTOR를 사용하여 작업을 시도해 볼 수 있습니다. 즉 .execute() 대신 .executeOnExecutor (AsyncTask.THREAD_POOL_EXECUTOR)를 사용하십시오. – AGDownie