簡體   English   中英

如何使用可能的響應類型來使用Retrofit響應?

[英]How to consume Retrofit response with possible response types?

我正在向我們的一個新項目介紹Retrofit 2。 當我使用Retrofit使用Web服務響應時,我遇到了一個特殊問題。

我正在使用改裝2.3.0,以及改裝gson轉換器2.3.0。

我的問題如下。 我正在使用的Web服務API將在請求為200-OK時響應2個可能的響應模型。

  1. Web服務處理了請求,並使用響應回調中定義的預期響應模型進行響應。
  2. Web服務無法處理請求並將異常模型作為響應發回。 仍然發送200-OK ......

相信我,我知道這里的問題...如果拋出Web服務異常,Web服務不應該響應成功...它應該響應500-5xx。 所有這一切都會變得容易多了。 無論如何,我需要遵循這個邏輯。

這就是我所擁有的並且它正在工作,但我認為有一個更好的方法,我不想一遍又一遍地編寫相同的代碼,每次我使用響應時執行強制轉換操作。 但是,我不確定這是哪種方法。

call.enqueue(new Callback<JsonObject>() {

     @Override
     public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
        //cast operation
        Gson gson = new Gson();
        ServiceException exception = gson.fromJson(response.body(), ServiceException.class);
        if(response.isSuccessful()){
             if(null != exception.getCode()){
                 //handleException(exception);
             }else{
                 //User authenticated successfully.

                 //cast operation
                 LoginResponseModel loginResponseModel = gson.fromJson(response.body(), LoginResponseModel.class);
                 //Perform actions with login response model.

                 //Launch dashboard if everything is ok.
                 Intent startDashboard = new Intent(LoginActivity.this, DashboardActivity.class);
                 startActivity(startDashboard);
             }
         }
     }

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

});

這是我理想的目標:

call.enqueue(new Callback<LoginResponseModel, ServiceException>() {

        @Override
        public void onResponse(Call<LoginResponseModel, ServiceException> call, Response<LoginResponseModel, ServiceException> response) {
               if(response instanceof ServiceException){
                   handleException(response);
               }else if(response instanceof LoginResponseModel){
                   //User authenticated successfully.
                   LoginResponseModel loginResponseModel = gson.fromJson(response.body(), LoginResponseModel.class);
                   //Perform actions with login response model.

                   //Launch dashboard if everything is ok.
                   Intent startDashboard = new Intent(LoginActivity.this, DashboardActivity.class);
                   startActivity(startDashboard);

                }
            }
        }

        @Override
        public void onFailure(Call<LoginResponseModel, ServiceException> call, Throwable t) {
            Log.e(TAG, t.getMessage());
        }

    });

我是否需要自定義Callback實現來實現我的目標? 我需要自定義響應攔截器嗎? 我需要自定義響應轉換器嗎? 任何更好的方法的想法?

我對Retrofit有點新,我沒有找到關於這種情況的很多信息。

嗯,使用try catch和處理gson JsonSyntaxException怎么JsonSyntaxException 像這樣的東西:

call.enqueue(new Callback<JsonObject>() {
        @Override
        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
            Gson gson = new Gson();
            try {
                ServiceException exception = gson.fromJson(response.body(), ServiceException.class);
                //handleException(exception);
            } catch (JsonSyntaxException e) {
                // Incorrect class, deserialize using LoginResponseModel 
                LoginResponseModel loginResponseModel = gson.fromJson(response.body(), LoginResponseModel.class);
                //User authenticated successfully.
                //Launch dashboard if everything is ok.
                Intent startDashboard = new Intent(LoginActivity.this, DashboardActivity.class);
                startActivity(startDashboard);
            }
        }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM