是否可以使用 android-priority-jobqueue 库来持久化 Volley API 请求,当它 returns 错误或作业对象转到 onCancel 时?
Is it possible to persist a Volley API request using android-priority-jobqueue library and when it returns Error or the Job object goes to onCancel?
我创建了一个将调用 API 请求的作业对象。但是我并不完全了解 Job 对象的生命周期。
这是我想要发生的事情
- 我希望在有 Internet 连接时发送 API 请求,如果没有,它将坚持在有 Internet 连接时发送。
- 如果 API 请求失败,因为在请求中间失去互联网连接或发生错误,我希望重新创建它并排队。
- 我还希望保留作业,并且仅当 returns 结果为成功或错误响应时才会从 JobManager 队列中删除。
- 无论如何,如何取消作业本身?
这是我的 class :
public class SampleRequestJob extends Job implements APIRequestListener {
public static final int PRIORITY = 1;
int id;
public SampleRequestJob(Context context, String id) {
super(new Params(PRIORITY).requireNetwork().persist());
this.id = id;
}
@Override
public void onAdded() {
// TODO : Show progress dialog in notifications
}
@Override
public void onRun() throws Throwable {
// call API Request
RequestManager.sampleApiRequest(id).setResultListener(this);
}
@Override
protected void onCancel() {
}
@Override
protected boolean shouldReRunOnThrowable(Throwable throwable) {
// should it really be false? I don't know the conditions of this method
return false;
}
@Override
public void OnRequestSuccess(APIRequestType type, JsonNode response) {
// TODO : Hide progress dialog in notifications
}
@Override
public void OnRequestError(APIRequestType type, VolleyError error) {
// TODO : Hide progress dialog in notifications, show error in notification panel.
// If error is timeout, no connection error, network error, or server error create job and add to jobmanager queue again
}
}
- 创建您的 'request' 对象
Serializable
,它会坚持并在给定参数通过后恢复(连接已恢复)。
- 您可以在
Job
(getRetryLimit
) 上定义重试次数。要触发重试,必须在 onRun
内抛出异常。否则,您可以 post/broadcast 并将事件发送到一个组件,该组件将再次重新排队对象。
- 作业将在
onRun
成功完成后删除,因此只要确保其中没有抛出异常就足够了。
- 看看here。同样,将异常或事件发布到触发器。
希望对您有所帮助。
我创建了一个将调用 API 请求的作业对象。但是我并不完全了解 Job 对象的生命周期。
这是我想要发生的事情
- 我希望在有 Internet 连接时发送 API 请求,如果没有,它将坚持在有 Internet 连接时发送。
- 如果 API 请求失败,因为在请求中间失去互联网连接或发生错误,我希望重新创建它并排队。
- 我还希望保留作业,并且仅当 returns 结果为成功或错误响应时才会从 JobManager 队列中删除。
- 无论如何,如何取消作业本身?
这是我的 class :
public class SampleRequestJob extends Job implements APIRequestListener {
public static final int PRIORITY = 1;
int id;
public SampleRequestJob(Context context, String id) {
super(new Params(PRIORITY).requireNetwork().persist());
this.id = id;
}
@Override
public void onAdded() {
// TODO : Show progress dialog in notifications
}
@Override
public void onRun() throws Throwable {
// call API Request
RequestManager.sampleApiRequest(id).setResultListener(this);
}
@Override
protected void onCancel() {
}
@Override
protected boolean shouldReRunOnThrowable(Throwable throwable) {
// should it really be false? I don't know the conditions of this method
return false;
}
@Override
public void OnRequestSuccess(APIRequestType type, JsonNode response) {
// TODO : Hide progress dialog in notifications
}
@Override
public void OnRequestError(APIRequestType type, VolleyError error) {
// TODO : Hide progress dialog in notifications, show error in notification panel.
// If error is timeout, no connection error, network error, or server error create job and add to jobmanager queue again
}
}
- 创建您的 'request' 对象
Serializable
,它会坚持并在给定参数通过后恢复(连接已恢复)。 - 您可以在
Job
(getRetryLimit
) 上定义重试次数。要触发重试,必须在onRun
内抛出异常。否则,您可以 post/broadcast 并将事件发送到一个组件,该组件将再次重新排队对象。 - 作业将在
onRun
成功完成后删除,因此只要确保其中没有抛出异常就足够了。 - 看看here。同样,将异常或事件发布到触发器。
希望对您有所帮助。