android - 如何通过改造创建新的回调

标签 android retrofit2

我在我的应用程序中使用 Retrofit 从他的服务器下载一些 JSON。但有时当我从服务器请求重复数据时,它会返回带有空体的响应代码 208,但在 200 中我有一个不为空的响应体。 在 null body 中,改造转到 onFailure 方法。我的问题就在这里! 我想知道请求失败的原因(这是因为 208(在我的情况下)或其他原因)。如何在 OnFailure 方法中获取响应代码?

我试着像这样实现我自己的回调方法:

public class DefaultCallback<T> implements Callback<T> {

  private static final String TAG = "LOGO_SendReport";
  private Callback<T> callback;

  public DefaultCallback(Callback<T> callback) {
    this.callback = callback;
  }

  @Override
  public void onResponse(Call<T> call, Response<T> response) {
    if (response.body() == null) {

      callback.onFailure(call, new NullPointerException("Empty response"));
      Log.e(TAG, "Response code is: " + response.code());

    } else {
      callback.onResponse(call, response);
    }
  }

  @Override
  public void onFailure(Call<T> call, Throwable t) {
    Log.e(TAG, t.toString());
    callback.onFailure(call, t);
  }
}

这是我的电话:

 @POST("office/report")
  Call<ResponseSendReport> sendReport(@Body SendReport pendingList);

这是我的 final方法:

    final APIService service = ServiceGenerator.createService(APIService.class, App.Auth);

            Call<ResponseSendReport> call = service.sendReport(sendReport);

            call.enqueue(new Callback<ResponseSendReport>() {
              @Override
              public void onResponse(Call<ResponseSendReport> call, Response<ResponseSendReport> response) {

                Log.i("LOGO_SendReport", "code is:  " + response.code());


                if (response.code() == 200) {


              }

              @Override
              public void onFailure(Call<ResponseSendReport> call, Throwable t) {

                /*When Response code is 208 Goes here and I want to get 
                 response code 
                 if it is 208 to handle it! */
              }
            });

感谢您的帮助。

最佳答案

您可以创建抽象类并处理案例。

public abstract class DefaultCallback<T> implements Callback<T> {

   @SuppressWarnings({"ConstantConditions", "unchecked"})
   @Override public void onResponse(@NonNull Call<T> call, Response<T> response) {
       if (response.isSuccessful()) {
           onSuccess(response.body(), response.code());
       } else {
           onError(response.errorBody(), response.code());
       }
   }

   @Override public void onFailure(@NonNull Call<T> call, @NonNull Throwable throwable) {
       onError(null, -1);
   }

   public abstract void onSuccess(@NonNull final T response, int code);

   public abstract void onError(@Nullable ResponseBody body, int code);
}

关于android - 如何通过改造创建新的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46805320/

相关文章:

android - 在 Android 中模拟低电量和低内存

android - Retrofit 和 RecyclerView 在启动时不显示项目

android - 如何让 Facebook 每次都让我登录,以便我可以调试登录过程?

java - Retrofit 2通过MS graph api发送邮件返回参数 'Message'的值为空

android - RXJava。确定 for 循环内的所有 obervables 何时完成

javascript - JQueryMobile 1.4.2 + Android 2.2/2.3 表格

Android:通过改造在ApiService中选择一部分response json

android - 改造 2 队列等待大小

android - Retrofit 2 有两条动态路径

android - 如何使用 Retrofit 和 RXJava 调用相同的 API N 次。我现在使用以下方法