gpt4 book ai didi

android - 是否可以使用 android-priority-jobqueue 库保留 Volley API 请求,以及当它返回错误或作业对象转到 onCancel 时?

转载 作者:行者123 更新时间:2023-11-29 01:29:09 25 4
gpt4 key购买 nike

我创建了一个将调用 API 请求的作业对象。但我并不完全了解 Job 对象的生命周期。

这是我想要发生的事情

  1. 我希望在有 Internet 连接时发送 API 请求,如果没有,它会坚持在有 Internet 连接时发送。
  2. 如果 API 请求因在请求过程中失去互联网连接或发生错误而失败,我希望重新创建并排队。
  3. 我还希望保留作业,并且只有在它返回成功或错误响应的结果时才会从 JobManager 队列中删除。
  4. 无论如何,您如何取消 Job 本身?

这是我的类(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
}
}

最佳答案

  1. 使您的“请求”对象可序列化,它会持续存在并在给定参数通过后恢复(连接已恢复)。
  2. 您可以在Job (getRetryLimit) 上定义重试次数。要触发重试,必须在 onRun 中抛出异常。否则,您可以发布/广播和事件到一个组件,该组件将再次重新排队对象。
  3. 作业将在 onRun 成功完成后删除,因此只要确保其中没有抛出异常就足够了。
  4. 看看here .同样,将异常或事件发布到触发器。

希望对您有所帮助。

关于android - 是否可以使用 android-priority-jobqueue 库保留 Volley API 请求,以及当它返回错误或作业对象转到 onCancel 时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32284754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com