Reputation: 1321
I'm calling an API my local server to verify if error 400 is occurring. This error occurs as expected. In this moment I want to get the value. It's a JSON object. The message from server is coming but the value at some moment disappears. I know ths because I'm debuggin the app. In the very first response from response.errorBody().string() message from Retrofit the string value is there. But right after the response the values becomes null. I put in image to illustrate what is going on.
And my code is:
public class ServerResponse implements Parcelable {
@SerializedName("message")
private String message;
public ServerResponse(){}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(message);
}
public ServerResponse(String message){
this.message = message;
}
private ServerResponse(Parcel parcel){
message = parcel.readString();
}
public static final Parcelable.Creator<ServerResponse> CREATOR =
new Parcelable.Creator<ServerResponse>() {
public ServerResponse createFromParcel(Parcel in) {
return new ServerResponse(in);
}
public ServerResponse[] newArray(int size) {
return new ServerResponse[size];
}
};
public String getMessage(){
return message;
}
}
And this is the another piece of code:
super.data.restApi(WorkOrderAPI.class)
.answerWorkOrder(
t,
workOrderId,
request.getQuestionFormId(),
isOffline ? 1 : 0,
request)
.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
if (response.isSuccessful()) {
onResult.onSuccessful(null);
} else {
try {
if(response.code() == 400) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new DateTypeDeserializer())
.create();
ServerResponse serverResponse = gson.fromJson(response.errorBody().charStream(), ServerResponse.class);
onResult.onUnsuccessful(new DefaultError(response.code(), serverResponse.getMessage()));
}
Why after the first line of code the value disappears?
Upvotes: 0
Views: 137
Reputation: 1321
Found the error. Like respondents said I'm consuming it twice. I'm consuming it on debug. See it on the picture.
Upvotes: 0
Reputation: 13321
From the documentation:
The response body can be consumed only once.
You are consuming it twice. The first time it return the value and the second time it's null.
Upvotes: 2
Reputation: 3869
Basically what happen is:
In the first if(response.code() == 400)
you get the errorBody and you ask Gson to parse it. So now the JSON has been consumed, it's gone.
Then when the second if(response.code() == 400)
is reached there is no JSON anymore.
The question is: why do you want to parse the errorBody twice?
Just do:
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
if (response.isSuccessful()) {
onResult.onSuccessful(null);
} else {
try {
if(response.code() == 400) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new DateTypeDeserializer())
.create();
ServerResponse serverResponse = gson.fromJson(response.errorBody().charStream(), ServerResponse.class);
onResult.onUnsuccessful(new DefaultError(response.code(), serverResponse.getMessage()));
}
}
// ... catch...
}
}
Upvotes: 1