In my current android project I'm sending some data to my web server with the new Retrofit 2.0 library. I only want to run a script there and the server does not return anything. I thought I can do this with this method:
@POST("persons/insert.php")
Call<Void> insert(@Field("Name") String name);
My problem now is, that Retrofit jumps to the onFailure method although the server returns 200. That happens because nothing can be converted to Void. So how should I fix this method? I want that the onResponse method is called if the server sends 200 back and the onFailure if an error occurred. How can I achieve that?
You can just return a ResponseBody , which will bypass parsing the response. Even better: Use Void which not only has better semantics but is (slightly) more efficient in the empty case and vastly more efficient in a non-empty case (when you just don't care about body).
You can have it return a ResponseBody.
@POST("persons/insert.php")
Call<ResponseBody> insert(@Field("Name") String name);
You will still get onResponse and onFailure, but it will not try to deserialize the stream to an object.
You can completely avoid the response body.
In beta2, you can do Call<Void> to discard the response body and just obtain the http status code.
@POST("persons/insert.php")
Call<Void> insert(@Field("Name") String name);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With