How can I handle empty response body with Retrofit 2?

Edit: As Jake Wharton points out, @GET(“/path/to/get”) 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. @GET(“/path/to/get”) Call<ResponseBody> getMyData(/* your args here */); Then in your call, Call<ResponseBody> dataCall = myApi.getMyData(); dataCall.enqueue(new Callback<ResponseBody>() { @Override … Read more

Adding header to all request with Retrofit 2

OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); httpClient.addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request().newBuilder().addHeader(“parameter”, “value”).build(); return chain.proceed(request); } }); Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(url).client(httpClient.build()).build();

Unable to create converter for my class in Android Retrofit library

If anyone ever comes across this in the future because you are trying to define your own custom converter factory and are getting this error, it can also be caused by having multiple variables in a class with a misspelled or the same serialized name. IE: public class foo { @SerializedName(“name”) String firstName; @SerializedName(“name”) String … Read more

POST Multipart Form Data using Retrofit 2.0 including image

There is a correct way of uploading a file with its name with Retrofit 2, without any hack: Define API interface: @Multipart @POST(“uploadAttachment”) Call<MyResponse> uploadAttachment(@Part MultipartBody.Part filePart); // You can add other parameters too Upload file like this: File file = // initialize file here MultipartBody.Part filePart = MultipartBody.Part.createFormData(“file”, file.getName(), RequestBody.create(MediaType.parse(“image/*”), file)); Call<MyResponse> call = … Read more

Retrofit 2.0 how to get deserialised error response.body

I currently use a very easy implementation, which does not require to use converters or special classes. The code I use is the following: public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { DialogHelper.dismiss(); if (response.isSuccessful()) { // Do your success stuff… } else { try { JSONObject jObjError = new JSONObject(response.errorBody().string()); Toast.makeText(getContext(), jObjError.getJSONObject(“error”).getString(“message”), Toast.LENGTH_LONG).show(); } catch … Read more

Refreshing OAuth token using Retrofit without modifying all calls

Please do not use Interceptors to deal with authentication. Currently, the best approach to handle authentication is to use the new Authenticator API, designed specifically for this purpose. OkHttp will automatically ask the Authenticator for credentials when a response is 401 Not Authorised retrying last failed request with them. public class TokenAuthenticator implements Authenticator { … Read more

Retrofit 2 – Dynamic URL

I think you are using it in wrong way. Here is an excerpt from the changelog: New: @Url parameter annotation allows passing a complete URL for an endpoint. So your interface should be like this: public interface APIService { @GET Call<Users> getUsers(@Url String url); }

When should one use RxJava Observable and when simple Callback on Android?

For simple networking stuff, the advantages of RxJava over Callback is very limited. The simple getUserPhoto example: RxJava: api.getUserPhoto(photoId) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<Photo>() { @Override public void call(Photo photo) { // do some stuff with your photo } }); Callback: api.getUserPhoto(photoId, new Callback<Photo>() { @Override public void onSuccess(Photo photo, Response response) { } }); The RxJava … Read more

How to POST raw whole JSON in the body of a Retrofit request?

The @Body annotation defines a single request body. interface Foo { @POST(“/jayson”) FooResponse postJson(@Body FooRequest body); } Since Retrofit uses Gson by default, the FooRequest instances will be serialized as JSON as the sole body of the request. public class FooRequest { final String foo; final String bar; FooRequest(String foo, String bar) { this.foo = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)