Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit, callback for 204 No Content response?

On Android, I initially implemented a Retrofit interface like this:

@DELETE(USER_API_BASE_URL + "/{id}")
public void deleteUser(@Path("id") String id, Callback<User> callback);

The server returns 204 NO CONTENT upon a successful deletion. This was causing the callback to trigger failure, with retrofit.RetrofitError: End of input at character 0 of, as it was expecting a User object back with the response.

I then rewrote it like this, using Void instead of User:

@DELETE(USER_API_BASE_URL + "/{id}")
public void deleteUser(@Path("id") String id, Callback<Void> callback);  <-- VOID

But I am getting the same error from the callback. What is the proper way to fix this? Thank you.

like image 839
ticofab Avatar asked Jan 14 '15 10:01

ticofab


3 Answers

Retrofit 2.x no longer has a ResponseCallback as mentioned in the other answer. You want to use a Response<Void> type.

The RxJava declaration:

@PUT Observable<Response<Void>> foo();

The standard declaration:

@PUT Call<Response<Void>> bar();
like image 102
Will Vanderhoef Avatar answered Nov 06 '22 19:11

Will Vanderhoef


The solution was pointed out by Jake Wharton in the comments. Use ResponseCallback.

EDIT: this response is no longer valid for Retrofit < 2.

like image 41
ticofab Avatar answered Nov 06 '22 19:11

ticofab


This is the kotlin way for the implementation to deal with HTTP 204 and no content.

@DELETE(USER_API_BASE_URL + "/{id}")
suspend fun deleteuser(@HeaderMap headers: Map<String, String>, 
                       @Path("id") id: String)
: Response<Void>
like image 35
flame3 Avatar answered Nov 06 '22 17:11

flame3