我正在尝试通过每次请求失败时使用handler.postDelayed(...)
调度线程来重试失败的http调用来实现指数退避。问题是我从IntentService这样做,它在调度第一个线程后死亡,因此处理程序无法调用自身。我收到以下错误:
java.lang.IllegalStateException: Handler (android.os.Handler) {2f31b19b} sending message to a Handler on a dead thread
我使用IntentService的课程:
@Override
protected void onHandleIntent(Intent intent) {
......
Handler handler = new Handler();
HttpRunnable httpRunnable = new HttpRunnable(info, handler);
handler.postDelayed(httpRunnable, 0);
}
我的自定义Runnable:
public class HttpRunnable implements Runnable {
private String info;
private static final String TAG = "HttpRunnable";
Handler handler = null;
int maxTries = 10;
int retryCount = 0;
int retryDelay = 1000; // Set the first delay here which will increase exponentially with each retry
public HttpRunnable(String info, Handler handler) {
this.info = info;
this.handler = handler;
}
@Override
public void run() {
try {
// Call my class which takes care of the http call
ApiBridge.getInstance().makeHttpCall(info);
} catch (Exception e) {
Log.d(TAG, e.toString());
if (maxTries > retryCount) {
Log.d(TAG,"%nRetrying in " + retryDelay / 1000 + " seconds");
retryCount++;
handler.postDelayed(this, retryDelay);
retryDelay = retryDelay * 2;
}
}
}
}
有没有办法让我的处理程序保持活力?以指数退避方式安排我的http重试的最佳/最干净方法是什么?
答案 0 :(得分:4)
使用IntentService
的主要优点是它可以在其onHandleIntent(Intent intent)
方法中处理所有背景线程。在这种情况下,您没有理由自己管理处理程序。
在这里,您可以使用AlarmManager
来安排向服务传递意图。您可以将重试信息保留在要传递的意图中。
我在想这样的事情:
public class YourService extends IntentService {
private static final String EXTRA_FAILED_ATTEMPTS = "com.your.package.EXTRA_FAILED_ATTEMPTS";
private static final String EXTRA_LAST_DELAY = "com.your.package.EXTRA_LAST_DELAY";
private static final int MAX_RETRIES = 10;
private static final int RETRY_DELAY = 1000;
public YourService() {
super("YourService");
}
@Override
protected final void onHandleIntent(Intent intent) {
// Your other code obtaining your info string.
try {
// Make your http call.
ApiBridge.getInstance().makeHttpCall(info);
} catch (Exception e) {
// Get the number of previously failed attempts, and add one.
int failedAttempts = intent.getIntExtra(EXTRA_FAILED_ATTEMPTS, 0) + 1;
// if we have failed less than the max retries, reschedule the intent
if (failedAttempts < MAX_RETRIES) {
// calculate the next delay
int lastDelay = intent.getIntExtra(EXTRA_LAST_DELAY, 0);
int thisDelay;
if (lastDelay == 0) {
thisDelay = RETRY_DELAY;
} else {
thisDelay = lastDelay * 2;
}
// update the intent with the latest retry info
intent.putExtra(EXTRA_FAILED_ATTEMPTS, failedAttempts);
intent.putExtra(EXTRA_LAST_DELAY, thisDelay);
// get the alarm manager
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// make the pending intent
PendingIntent pendingIntent = PendingIntent
.getService(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// schedule the intent for future delivery
alarmManager.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + thisDelay, pendingIntent);
}
}
}
}
这简单地让你正在使用的IntentService在后台执行调用,然后在每次失败时调度要重新发送的意图,为它添加额外的次数,重试次数以及重试多长时间最后的重试延迟是。
注意:如果您尝试向此服务发送多个意图并且多个意图失败并且必须使用AlarmManager
重新安排,则只有根据{{认为意图相等才会传递最新意图1}}。如果您的意图与附加的附加内容相同,则这将是一个问题,并且您必须在创建Intent.filterEquals(Intent intent)
时为每个重新安排的意图使用唯一的requestCode。这些方面的东西:
PendingIntent
我想您可以使用共享首选项来存储请求代码,每次必须安排重试时,该代码会递增。