使用 OneTimeWorkRequest 重新发送 Retrofit API 调用
Resend Retrofit API call with a OneTimeWorkRequest
我卡住了,无法让我的 OneTimeWorkRequest 正常工作。
我收到了这个 Retrofit API 调用,我想重新发送到后端,以防 phone 上没有网络连接或类似功能。我想不通的是如何将呼叫转给工作人员。
我有以下代码:
这是 API 服务:
public void sendMessage(Context context, Message message){
ListService listService = ServiceBuilder.getInstance(context).buildService(ListService.class);
Call<Void> createRequest = listService.sendMessage(message);
createRequest.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
if(response.isSuccessful()) {
Toast toast = new Toast(context);
toast.setText("Thank you for your message!");
toast.show();
}
else{
Toast toast = new Toast(context);
toast.setText("Could not send message!");
toast.show();
}
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
Toast toast = new Toast(context);
toast.setText("Could not send message!");
toast.show();
}
});
}
主要工作人员class:
Constraints constraints4 = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
OneTimeWorkRequest sendFeedbackWorker = new OneTimeWorkRequest.Builder(SendMessageWorker.class)
.setInitialDelay(15, TimeUnit.MINUTES)
.setConstraints(constraints4)
.build();
WorkManager.getInstance().enqueue(sendMessageWorker);
工人class:
public class SendMessageWorker extends Worker {
private static final String TAG = "SendMessageWorker";
public SendMessageWorker(
@NonNull
Context context,
@NonNull
WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@NotNull
@Override
public Result doWork() {
//What do I do here?
return Result.success();
}
}
我错过了什么,难道只是 Worker class?它是我需要的应用程序上下文吗?如果可以,我该如何通过?
我把所有的信息都作为putStrings,转换了日期。
Data myFeedbackMessage = new Data.Builder()
.putString("myFeedbackMessageId", message.getId())
.putString("myFeedbackMessage", message.getFeedbackMessage())
.putString("myFeedbackPerson", message.getFeedbackPersonName())
.putString("myFeedbackCreated",message.getCreated().toString())
.build();
我卡住了,无法让我的 OneTimeWorkRequest 正常工作。
我收到了这个 Retrofit API 调用,我想重新发送到后端,以防 phone 上没有网络连接或类似功能。我想不通的是如何将呼叫转给工作人员。
我有以下代码:
这是 API 服务:
public void sendMessage(Context context, Message message){
ListService listService = ServiceBuilder.getInstance(context).buildService(ListService.class);
Call<Void> createRequest = listService.sendMessage(message);
createRequest.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
if(response.isSuccessful()) {
Toast toast = new Toast(context);
toast.setText("Thank you for your message!");
toast.show();
}
else{
Toast toast = new Toast(context);
toast.setText("Could not send message!");
toast.show();
}
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
Toast toast = new Toast(context);
toast.setText("Could not send message!");
toast.show();
}
});
}
主要工作人员class:
Constraints constraints4 = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
OneTimeWorkRequest sendFeedbackWorker = new OneTimeWorkRequest.Builder(SendMessageWorker.class)
.setInitialDelay(15, TimeUnit.MINUTES)
.setConstraints(constraints4)
.build();
WorkManager.getInstance().enqueue(sendMessageWorker);
工人class:
public class SendMessageWorker extends Worker {
private static final String TAG = "SendMessageWorker";
public SendMessageWorker(
@NonNull
Context context,
@NonNull
WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@NotNull
@Override
public Result doWork() {
//What do I do here?
return Result.success();
}
}
我错过了什么,难道只是 Worker class?它是我需要的应用程序上下文吗?如果可以,我该如何通过?
我把所有的信息都作为putStrings,转换了日期。
Data myFeedbackMessage = new Data.Builder()
.putString("myFeedbackMessageId", message.getId())
.putString("myFeedbackMessage", message.getFeedbackMessage())
.putString("myFeedbackPerson", message.getFeedbackPersonName())
.putString("myFeedbackCreated",message.getCreated().toString())
.build();