【问题标题】:Handling Retrofit status codes without a pojo class处理没有 pojo 类的改造状态代码
【发布时间】:2017-05-23 18:49:02
【问题描述】:

我从我的服务器收到以下响应:状态代码201 Created。 没有实际响应(返回对象等),所以不需要创建 POJO 类。

所以,我不知道在不创建 POJO 类的情况下应该如何处理此状态码。是否有任何选项可以在不使用 POJO 类的情况下编写代码?

【问题讨论】:

标签: java android retrofit


【解决方案1】:

Retrofit API 有 Response 类,可以封装您的响应。

只要您不想打扰响应数据,您可以将服务实现为:

interface CustomService {
    @GET("whatever")
    Call<Response<Void>> getAll();

    // Or using RxJava:
    @GET("whatever")
    Single<Response<Void>> getRxAll();
}

然后实现你的回调:

private Callback<Response<Void>> responseHandler = new Callback<Response<Void>>() {
  @Override
  public void onResponse(Call<Response<Void>> call, Response<Response<Void>> response) {
    final int code = response.code();
    // TODO: Do whatever you want with the response code.
  }

  @Override
  public void onFailure(Call<Response<Void>> call, Throwable t) {
      // TODO: Handle failure.
  }
}

或反应式消费者:

private Consumer<Response<Void>> responseRxHandler = new Consumer<Response<Void>>() {
  @Override
  public void accept(Response<Void> response) throws Exception {
      final int responseCode = response.code();
      // TODO: Do whatever you want with the response code.
  }
};

调试结果:

【讨论】:

    【解决方案2】:

    你可以试试下面的代码。

    可以通过使用ResponseBody格式获取不带POJO类的响应,然后就可以像普通的JSON解析一样正常解析了。

    API 调用:

    Call<ResponseBody> call = service.callLogin(AppConstants.mApiKey, model_obj);
    
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if(response.code() == 201)
        {
         JSONObject jobjresponse = null;
        try {
                jobjresponse = new JSONObject(mResponse.body().string());
                String status = jobjresponse.getString("status");
                JSONObject  result = jobjresponse.getJSONObject("results");
                        String  msg = result.getString(“msg”);  
    } catch (JSONException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }       
    
            }
    
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
    
            }
        });
    

    改造接口类:

    public interface RetrofitInterface {
    @Headers({"Content-Type: application/json", "Cache-Control: max-age=640000"})
    @POST("v1/auth/")
    public Call<ResponseBody> callLogin(@Query("key") String key, @Body LoginModel body);
    
    public static final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(“base url”)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    }
    

    示例响应:

    { "status":"true", "result":{"msg”:”created successfully”} }
    

    【讨论】:

      猜你喜欢
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      • 2014-09-06
      • 2021-08-11
      • 2022-06-21
      • 2021-01-23
      • 2020-07-14
      相关资源
      最近更新 更多