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

Logging with Retrofit 2

In Retrofit 2 you should use HttpLoggingInterceptor. Add dependency to build.gradle. Latest version as of October 2019 is: implementation ‘com.squareup.okhttp3:logging-interceptor:4.2.1’ Create a Retrofit object like the following: HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(“https://backend.example.com”) .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); return retrofit.create(ApiClient.class); In case of deprecation warnings, simply … Read more

How to create custom GsonConverter from following response?

Use these Model classes and pass your response directly through Gson com.example.ACCESSORIES.java package com.example; import java.util.List; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; public class ACCESSORIES { @SerializedName(“accessories”) @Expose private List<Accessory> accessories = null; public List<Accessory> getAccessories() { return accessories; } public void setAccessories(List<Accessory> accessories) { this.accessories = accessories; } } com.example.Accessory.java package com.example; import java.util.List; import com.google.gson.annotations.Expose; … Read more