How to get string response from Retrofit2?

Add Retrofit2 and ScalarsConverterFactory to your Retrofit.Builder. adapterBuilder = new Retrofit.Builder() .addConverterFactory(ScalarsConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create()); To use ScalarsCoverter add following dependency to your build graddle implementation ‘com.squareup.retrofit2:converter-scalars:2.9.0’ implementation ‘com.squareup.retrofit2:retrofit:2.9.0’ //Adding Retrofit2 For API Call use: Call <String> ***** Android Code : .enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { Log.i(“Response”, response.body().toString()); //Toast.makeText() if (response.isSuccessful()){ … Read more

How to get Retrofit success response status codes

As per Retrofit 2.0.2, the call is now @Override public void onResponse(Call<YourModel> call, Response<YourModel> response) { if (response.code() == 200) { // Do awesome stuff } else { // Handle other response codes } } Hope it helps someone 🙂 EDIT: Many apps could also benefit from just checking for success (response code 200-300) in … Read more

How to make multiple request and wait until data is come from all the requests in retrofit 2.0 – android

The clean and neat approach to wait until all your requests will be done is to use Retrofit2 in conjunction with RxJava2 and its zip function. What zip does is basically constructs new observable that waits until all your retrofit Observable requests will be done and then it will emit its own result. Here is … 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

Retrofit2 error java.io.EOFException: End of input at line 1 column 1

just return void instead, if the body is empty @PATCH(“alerts/{alert_id}/accept”) Call<Void> accept_invited_alerts(@Header(“X-Api-Token”) String api_token, @Path(“alert_id”) int alert_id); for retrofit with Rx java you can use something like this @PATCH(“alerts/{alert_id}/accept”) Observable<Response<Void>> accept_invited_alerts(@Header(“X-Api-Token”) String api_token, @Path(“alert_id”) int alert_id); EDIT: For kotlin @PATCH(“alerts/{alert_id}/accept”) fun accept_invited_alerts(@Header(“X-Api-Token”) api_token: String, @Path(“alert_id”) alert_id: Int): Call<Unit> and @PATCH(“alerts/{alert_id}/accept”) fun accept_invited_alerts(@Header(“X-Api-Token”) api_token: String, @Path(“alert_id”) … Read more

Set dynamic base url using Retrofit 2.0 and Dagger 2

Support for this use-case was removed in Retrofit2. The recommendation is to use an OkHttp interceptor instead. HostSelectionInterceptor made by swankjesse import java.io.IOException; import okhttp3.HttpUrl; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; /** An interceptor that allows runtime changes to the URL hostname. */ public final class HostSelectionInterceptor implements Interceptor { private volatile String host; public … Read more

Retrofit2 Android: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

When you say “This code is working with this payload:… but not with this one:…” that’s expected and that’s how it’s suppose to work. In fact the error message tells you that while converting the json to a java object the call expected an array in the json but got an object instead. This call: … 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

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

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