Parse JSON array response using Retrofit & Gson

Please try to use this one @FormUrlEncoded @POST(“api/sponsors”) Call<List<SponsorsResult>> getStatesAndDistrict( @Field(“xyz”) String field1 ); Call <List<SponsorsResult>> call = service.getSponsorsValue(); call.enqueue(new Callback<List<SponsorsResult>>() { @Override public void onResponse(Call<List<SponsorsResult>> call, Response<List<SponsorsResult>> response) { List<SponsorsResult> rs = response.body(); } @Override public void onFailure(Call<List<SponsorsResult>> call, Throwable t) { } }); class SponsorsResult { @SerializedName(“sponsors”) private List<SponsorsValue> sponsors; public List<SponsorsValue> getSponsors() … Read more

Disable SSL certificate check in retrofit library

Use this class to get unsafe Retrofit instance. I have included imports to avoid confusion. import java.security.cert.CertificateException; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import okhttp3.OkHttpClient; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; import view.utils.AppConstants; /** * Created by Hitesh.Sahu on 11/23/2016. */ public class NetworkHandler { public static Retrofit getRetrofit() { return … 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

Retrofit – Multipart request: Required MultipartFile parameter ‘file’ is not present

You can try the following sample code. In this demo app, we will upload a photo after selecting from the Gallery. Hope it helps! build.gradle file: dependencies { … compile ‘com.squareup.retrofit2:retrofit:2.0.1’ compile ‘com.squareup.retrofit2:converter-gson:2.0.1’ … } WebAPIService.java file: public interface WebAPIService { @Multipart @POST(“/api/fileupload”) Call<ResponseBody> postFile(@Part MultipartBody.Part file, @Part(“description”) RequestBody description); } FileActivity.java file: … import … Read more

Retrofit 2: Get JSON from Response body

Use this link to convert your JSON into POJO with select options as selected in image below You will get a POJO class for your response like this public class Result { @SerializedName(“id”) @Expose private Integer id; @SerializedName(“Username”) @Expose private String username; @SerializedName(“Level”) @Expose private String level; /** * * @return * The id */ … Read more

Why does PDF is not able to upload in PHP API for Android Pie, Q and R using Retrofit 2 in Android/Java? [Not solved]

You api accept only two parameters but you passed three parameters that’s why you getting error. so API method should be @Multipart @POST(“eligibity/auth/attachment/upload/add”) Call<ResponseBody> uploadFile( @Part(“body”) RequestBody description, @Part MultipartBody.Part file ); And update your uploadPDF() like below private void uploadPDF(String path) { //json data JSONObject sJsonAttachment = new JSONObject(); JSONObject body = new JSONObject(); … Read more

Is it possible to show progress bar when upload image via Retrofit 2?

First of all, you should use Retrofit 2 version equal to or above 2.0 beta2. Second, create new class extends RequestBody: public class ProgressRequestBody extends RequestBody { private File mFile; private String mPath; private UploadCallbacks mListener; private String content_type; private static final int DEFAULT_BUFFER_SIZE = 2048; public interface UploadCallbacks { void onProgressUpdate(int percentage); void onError(); … Read more

How to upload an image file in Retrofit 2

@Multipart @POST(“user/updateprofile”) Observable<ResponseBody> updateProfile(@Part(“user_id”) RequestBody id, @Part(“full_name”) RequestBody fullName, @Part MultipartBody.Part image, @Part(“other”) RequestBody other); //pass it like this File file = new File(“/storage/emulated/0/Download/Corrections 6.jpg”); RequestBody requestFile = RequestBody.create(MediaType.parse(“multipart/form-data”), file); // MultipartBody.Part is used to send also the actual file name MultipartBody.Part body = MultipartBody.Part.createFormData(“image”, file.getName(), requestFile); // add another part within the multipart request … Read more