How to implement HttpRequestRetryHandler with Exponential Backoff?
android, httprequest, java
Solution
HttpRequestRetryHandler doesn't allow you that level of control; if you want to do something very specific like that, I'd recommend implementing something like a Handler where you can post Runnables to be executed with a delay, using for example Handler.postDelayed() with increasing delays as per your formula.
Handler mHandler = new Handler();
int mDelay = INITIAL_DELAY;
// try request
mHandler.postDelayed(mDelay, new Runnable() {
public void run() {
// try your request here; if it fails, then repost:
if (failed) {
mDelay *= 2; // or as per your formula
mHandler.postDelayed(mDelay, this);
}
else {
// success!
}
}
});
Problem
I wish to implement a HttpRequestRetryHandler for a HttpClient in case the request fails first time. I also wish to implement Exponential backoff for subsequent retries. Mathematically it can be implemented as E(c) = 1/2(2 power (c) -1) but I am struggling for quite some time now to implement it in the code with HttpRequestRetryHandler.