How can I handle empty response body with Retrofit 2?


As Jake Wharton points out,

Call<Void> getMyData(/* your args here */);

is the best way to go versus my original response —

You can just return a ResponseBody, which will bypass parsing the response.

Call<ResponseBody> getMyData(/* your args here */);

Then in your call,

Call<ResponseBody> dataCall = myApi.getMyData();
dataCall.enqueue(new Callback<ResponseBody>() {
    public void onResponse(Response<ResponseBody> response) {
        // use response.code, response.headers, etc.

    public void onFailure(Throwable t) {
        // handle failure

Leave a Comment