Reputation: 18118
i have a scenario that when you do a PUT on a end point and get an OK http code 200, then you dont expect a body. However if the server returns a http code representing an error, it send a error json as a body.
How do i handle both cases? it seems you can only handle one of the other
@PUT("/path/to/get")
Call<Response<<Void>> getMyData(/* your args here */);
if i get a errorResponse body, it will obviously wont get the data response
or
@PUT("/path/to/get")
Call<Response<ErrorResponse>> getMyData(/* your args here */);
If the response is good then it tries to convert a body that has nothing and results to a java.io.EOFException: End of input at line 1 column 1 path $
Upvotes: 0
Views: 3818
Reputation: 10106
Use ResponseBody
for such case:
service.getMyData().enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
gson.fromJson(response.body().charStream(), MyClass.class);
} else {
//Do something in case of error
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
Upvotes: 2