Skip to content Skip to sidebar Skip to footer

How To Get Both A Pojo And The Raw Response String Back With Retrofit?

FATAL EXCEPTION: main Process: com.packagename, PID: 11371 java.lang.IllegalStateException: Cannot read raw response body of a converted body. In retrofit, you can only ever read

Solution 1:

You could get the raw response body by adding an interceptor (https://square.github.io/okhttp/interceptors/) and copying the responsebody BufferedSource before returning the response. I'm having trouble seeing why someone would want to do this though.

Responseresponse= chain.proceed(request);
ResponseBodyresponseBody= response.body();

ByteArrayOutputStreamoutput=newByteArrayOutputStream();
responseBody.source().getBuffer().copyTo(output);
StringrawResponseBody= output.toString();

return response;

Solution 2:

example for model class:

publicclassPost {
    @SerializedName("text")
    privateString text;
    privateUser   user;

    publicStringgetText() {
        return text;
    }

    publicUsergetUser() {
        return user;
    }
}

classUser{
    @SerializedName("id")
    private int id;
    @SerializedName("name")
    privateString name;

    public int getId() {
        return id;
    }

    publicStringgetName() {
        return name;
    }
}}

for better answer: put your model class and onResponse method body in your question

Post a Comment for "How To Get Both A Pojo And The Raw Response String Back With Retrofit?"