Last active
November 9, 2020 16:48
-
-
Save pallavahooja/1aaff98c1a790db7ea96e0015d1ac2d3 to your computer and use it in GitHub Desktop.
Retrofit Retry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.goplus.in.v2.network; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
import retrofit2.Response; | |
/** | |
* Created by pallavahooja on 16/05/16. | |
*/ | |
public class APIHelper { | |
public static final int DEFAULT_RETRIES = 3; | |
public static <T> void enqueueWithRetry(Call<T> call, final int retryCount,final Callback<T> callback) { | |
call.enqueue(new RetryableCallback<T>(call, retryCount) { | |
@Override | |
public void onFinalResponse(Call<T> call, Response<T> response) { | |
callback.onResponse(call, response); | |
} | |
@Override | |
public void onFinalFailure(Call<T> call, Throwable t) { | |
callback.onFailure(call, t); | |
} | |
}); | |
} | |
public static <T> void enqueueWithRetry(Call<T> call, final Callback<T> callback) { | |
enqueueWithRetry(call, DEFAULT_RETRIES, callback); | |
} | |
public static boolean isCallSuccess(Response response) { | |
int code = response.code(); | |
return (code >= 200 && code < 400); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Call<BaseResponse> call = authService.initiateSignup(new RequestInitiateSignup(number, referral)); | |
APIHelper.enqueueWithRetry(call ,5,new Callback<BaseResponse>() { | |
@Override | |
public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) { | |
} | |
@Override | |
public void onFailure(Call<BaseResponse> call, Throwable t) { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.goplus.in.v2.network; | |
import android.util.Log; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
import retrofit2.Response; | |
/** | |
* Created by pallavahooja on 16/05/16. | |
*/ | |
public abstract class RetryableCallback<T> implements Callback<T> { | |
private int totalRetries = 3; | |
private static final String TAG = RetryableCallback.class.getSimpleName(); | |
private final Call<T> call; | |
private int retryCount = 0; | |
public RetryableCallback(Call<T> call, int totalRetries) { | |
this.call = call; | |
this.totalRetries = totalRetries; | |
} | |
@Override | |
public void onResponse(Call<T> call, Response<T> response) { | |
if (!APIHelper.isCallSuccess(response)) | |
if (retryCount++ < totalRetries) { | |
Log.v(TAG, "Retrying API Call - (" + retryCount + " / " + totalRetries + ")"); | |
retry(); | |
} else | |
onFinalResponse(call, response); | |
else | |
onFinalResponse(call,response); | |
} | |
@Override | |
public void onFailure(Call<T> call, Throwable t) { | |
Log.e(TAG, t.getMessage()); | |
if (retryCount++ < totalRetries) { | |
Log.v(TAG, "Retrying API Call - (" + retryCount + " / " + totalRetries + ")"); | |
retry(); | |
} else | |
onFinalFailure(call, t); | |
} | |
public void onFinalResponse(Call<T> call, Response<T> response) { | |
} | |
public void onFinalFailure(Call<T> call, Throwable t) { | |
} | |
private void retry() { | |
call.clone().enqueue(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Muchas gracias! está perfecto!!