Retrofit 2.0 how to get deserialised error response.body

前端 未结 21 2067
庸人自扰
庸人自扰 2020-11-28 02:46

I\'m using Retrofit 2.0.0-beta1.

In tests i have an alternate scenario and expect error HTTP 400

I would like to have retrofit.Respons

相关标签:
21条回答
  • 2020-11-28 03:04

    In https://stackoverflow.com/a/21103420/2914140 and https://futurestud.io/tutorials/retrofit-2-simple-error-handling this variant is shown for Retrofit 2.1.0.

    call.enqueue(new Callback<MyResponse>() {
        @Override
        public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
            if (response.isSuccessful()) {
                ...
            } else {
                Converter<ResponseBody, MyError> converter
                        = MyApplication.getRetrofit().responseBodyConverter(
                        MyError.class, new Annotation[0]);
                MyError errorResponse = null;
                try {
                    errorResponse = converter.convert(response.errorBody());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-28 03:05
    try{
                    ResponseBody response = ((HttpException) t).response().errorBody();
                    JSONObject json = new JSONObject( new String(response.bytes()) );
                    errMsg = json.getString("message");
                }catch(JSONException e){
                    return t.getMessage();
                }
                catch(IOException e){
                    return t.getMessage();
                }
    
    0 讨论(0)
  • 2020-11-28 03:06
    if(!response.isSuccessful()) {
        StringBuilder error = new StringBuilder();
        try {
            BufferedReader bufferedReader = null;
            if (response.errorBody() != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(
                        response.errorBody().byteStream()));
    
                String eLine = null;
                while ((eLine = bufferedReader.readLine()) != null) {
                    error.append(eLine);
                }
                bufferedReader.close();
            }
    
        } catch (Exception e) {
            error.append(e.getMessage());
        }
    
        Log.e("Error", error.toString());
    }
    
    0 讨论(0)
  • 2020-11-28 03:09
     @Override
     public void onResponse(Call<Void> call, retrofit2.Response<Void> response) {
                if (response.isSuccessful()) {
    
                //Do something if response is ok
                } else {
    
                    JsonParser parser = new JsonParser();
                    JsonElement mJson = null;
                    try {
                        mJson = parser.parse(response.errorBody().string());
                        Gson gson = new Gson();
                        MyError errorResponse = gson.fromJson(mJson, MyError.class);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
    
                }
    
    0 讨论(0)
  • 2020-11-28 03:09

    If you use Kotlin another solution could be just create extension function for Response class:

    inline fun <reified T>Response<*>.parseErrJsonResponse(): T?
    {
        val moshi = MyCustomMoshiBuilder().build()
        val parser = moshi.adapter(T::class.java)
        val response = errorBody()?.string()
        if(response != null)
            try {
                return parser.fromJson(response)
            } catch(e: JsonDataException) {
                e.printStackTrace()
            }
        return null
    }
    

    Usage

    val myError = response.parseErrJsonResponse<MyErrorResponse>()
    if(myError != null) {
       // handle your error logic here
       // ...
    }
    
    0 讨论(0)
  • 2020-11-28 03:10

    I did it this way for asynchronous calls using Retrofit 2.0-beta2:

    @Override
    public void onResponse(Response<RegistrationResponse> response, 
                           Retrofit retrofit) {
        if (response.isSuccess()) {
            // Do success handling here
        } else {
            try {
                MyError myError = (MyError)retrofit.responseConverter(
                        MyError.class, MyError.class.getAnnotations())
                    .convert(response.errorBody());
                // Do error handling here
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题