Retrofit 2.0 - How to get response body for 400 Bad Request error?

前端 未结 9 1880
旧巷少年郎
旧巷少年郎 2021-01-31 02:33

So when I make a POST API call to my server, I get a 400 Bad Request error with JSON response.

{
    \"userMessage\": \"Blah\",
    \"internalMessage\": \"Bad Re         


        
相关标签:
9条回答
  • 2021-01-31 03:09

    First step:

    Create your POJO class for error response. In my case, ApiError.java

    public class ApiError {
    
        @SerializedName("errorMessage")
        @Expose
        private String errorMessage;
    
        public String getErrorMessage() {
            return errorMessage;
        }
    
        public void setErrorMessage(String errorMessage) {
            this.errorMessage= errorMessage;
        }
    }
    

    Second Step:

    Write below code in your api callback.

    Call.enqueue(new Callback<RegistrationResponse>() {
         @Override
         public void onResponse(Call<RegistrationResponse> call, Response<RegistrationResponse> response) 
         {
             if (response.isSuccessful()) {
                 // do your code here
             } else if (response.code() == 400) {
                 Converter<ResponseBody, ApiError> converter =
                                ApiClient.retrofit.responseBodyConverter(ApiError.class, new Annotation[0]);
    
                        ApiError error;
    
                        try {
                            error = converter.convert(response.errorBody());
                            Log.e("error message", error.getErrorMessage());
                            Toast.makeText(context, error.getErrorMessage(), Toast.LENGTH_LONG).show();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
             }
         }
    
         @Override
         public void onFailure(Call<RegistrationResponse> call, Throwable t) {
             //do your failure handling code here
         }
    }
    

    Here ApiClient.retrofit is your retrofit instance which is static.

    0 讨论(0)
  • 2021-01-31 03:10

    Handle ErrorResponse with your class object

    Kotlin

    val errorResponse = Gson().fromJson(response.errorBody()!!.charStream(), ErrorResponse::class.java)
    

    Java

    ErrorResponse errorResponse = new Gson().fromJson(response.errorBody.charStream(),ErrorResponse.class)
    
    0 讨论(0)
  • 2021-01-31 03:15

    You can try the below code to get 400 response. You can get error response from errorBody() method.

    Call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            //get success and error response here
     if (response.code() == 400) {
                    if(!response.isSuccessful()) {
                        JSONObject jsonObject = null;
                        try {
                            jsonObject = new JSONObject(response.errorBody().string());
                            String userMessage = jsonObject.getString("userMessage");
                            String internalMessage = jsonObject.getString("internalMessage");
                            String errorCode = jsonObject.getString("errorCode");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
    
        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            //get failure response here
        }
    }
    }
    

    EDIT: Fixed method name from toString to string

    0 讨论(0)
提交回复
热议问题