我终于做了这样的事情,对任何有兴趣的人:
1
首先我做了一个抽象类CallbackWithRetry
public abstract class CallbackWithRetry implements Callback {
private static final int TOTAL_RETRIES = 3;
private static final String TAG = CallbackWithRetry.class.getSimpleName();
private final Call call;
private int retryCount = 0;
public CallbackWithRetry(Call call) {
this.call = call;
}
@Override
public void onFailure(Throwable t) {
Log.e(TAG, t.getLocalizedMessage());
if (retryCount++ < TOTAL_RETRIES) {
Log.v(TAG, "Retrying... (" + retryCount + " out of " + TOTAL_RETRIES + ")");
retry();
}
}
private void retry() {
call.clone().enqueue(this);
}
}
使用这个类我可以这样做: