티스토리 뷰
AsyncTask 는 Deprecated 되었습니다.
구현부
package com.winchester.retrofit.example.http.asyncTask;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.gson.JsonObject;
import com.winchester.retrofit.example.http.responseBody.RespPacket;
import java.io.IOException;
import java.lang.ref.WeakReference;
import retrofit2.Call;
import retrofit2.Response;
public class SendPacketTask extends AsyncTask<Call, Void, Response<RespPacket>> {
private WeakReference<ProgressDialog> progressDialog;
private NextAction nextAction;
@Nullable
private PacketListener packetListener;
private HttpPacketType packetType;
public SendPacketTask(ProgressDialog progressDialog,
@Nullable PacketListener packetListener,
@NonNull final HttpPacketType type,
NextAction action) {
this.progressDialog = new WeakReference<>(progressDialog);
this.packetListener = packetListener;
this.nextAction = action;
this.packetType = type;
}
@Override
protected void onPreExecute() {
progressDialog.get().setCancelable(false);
progressDialog.get().show();
}
@Override
protected Response<RespPacket> doInBackground(Call... params) {
try{
Call<RespPacket> call = params[0];
Response<RespPacket> response = call.execute();
return response;
}catch (IOException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Response<RespPacket> response) {
if(packetListener != null && response != null)
{
if( response.isSuccessful() )
{
packetListener.onSuccess(response.code(), response.body(), packetType);
}else {
packetListener.onFailure(response.code(), "err msg");
}
}
if(progressDialog.get() != null && progressDialog.get().isShowing())
progressDialog.get().dismiss();
if(nextAction != null)
nextAction.run();
}
@FunctionalInterface
public interface NextAction {
void run();
}
}
호출
public void sendPacketAsyncTask(Activity activity, String c_id, long t_id) {
ProgressDialog progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("sendPacketAsyncTask...");
Call<RespCarrierTracks> call = apiService.getCarriersTracks(c_id, t_id);
SendPacketTask sendTask = new SendPacketTask(
progressDialog,
packetListener,
HttpPacketType.RESPONSE_CARRIERS_TRACKS,
null);
sendTask.execute(call);
}
'Development Note > Android' 카테고리의 다른 글
[Android Java] 안드로이드 권한 요청 정리 (0) | 2020.08.10 |
---|---|
[Android Java] 구글 인앱결제 구현 방법 (0) | 2020.08.04 |
[Android Java] 안드로이드 millisecond to Date, String 로 변환 (0) | 2020.04.12 |
[Android Java] UTC Time 가져오기 (0) | 2020.04.12 |
[Android Java] 안드로이드 암호화(md5, sha256) (0) | 2020.04.12 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- Golang
- countryName
- NSUserDefaults 예제
- nsurl 에서 nsstring 변환
- LanguageCode
- dialog
- 퍼미션체크
- tempdir
- nsurl to nsstring convert
- countryCode
- convert nsurl to nsstring
- setCancelable vs setCanceledOnTouchOutside
- 절대값함수
- Error Domain=SKErrorDomain Code=0
- ioutil
- tempfile
- java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line
- Locale.getDefault().getLanguage()
- permissionCheck
- abs 함수
- abs
- DeviceModel
- iTunes Store에 연결할 수 없음
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
글 보관함