How to get response body to retrofit exception?

前端 未结 4 1142
梦如初夏
梦如初夏 2021-02-01 02:12

I am trying to connect to rest service via retrofit in android application. I am getting responses. But when there is some error response from the service, conversion exception

4条回答
  •  不知归路
    2021-02-01 02:42

    if error in String format:

    public Sring getErrorBodyAsString(RetrofitError error) {    
          return (String) error.getBodyAs(String.class)
    }
    

    if you need custom object:

    class ErrorResponse {
       @SerializedName("error")
       int errorCode;
    
       @SerializedName("msg")
       String msg;
    }
    
    public T getErrorBody(RetrofitError error, Class clazz) {    
          return (T) error.getBodyAs(clazz);
    }
    

提交回复
热议问题