How to set timeout in Retrofit-2.0+ android

Configure OkHttpClient for timeout option. Then use this as client for Retrofit.Builder. final OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(20, TimeUnit.SECONDS) .writeTimeout(20, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .build(); Use this okHttpClient for Retrofit#Builder Retrofit.Builder() .client(okHttpClient); Official OkHttp documentation about timeout is here

Why is RxJava often used with Retrofit?

Question Retrofit Already in run on background thread. Then why need another background task RxJava? I think most importanly, avoid nested callbacks(callback hell). e.g) Callback hell (Retrofit) public interface MyService { @GET(“users”) Call<List<UserModel>> getUser(); @GET(“userinfo”) Call<UserInfoModel> getUserInfoById(@Query(“id”) Integer id); } service.getUser().enqueue(new Callback<UserModel>() { @Override public void onResponse(Call<UserModel> call, Response<UserModel> response) { //process UserModel UserModel data … Read more

Android Retrofit 2, differences between addInterceptor & addNetworkInterceptor for editing responses

The differences are in the names. NetworkInterceptor hooks in at the network level and is an ideal place to put retry logic and anything that doesn’t rely on the actual content of the response. If what you do depends on the contents of the response (like in your case), using a ApplicationInterceptor is more useful, … Read more

Retrofit and OkHttp basic authentication

Find the Solution 1.Write a Interceptor class import java.io.IOException; import okhttp3.Credentials; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; public class BasicAuthInterceptor implements Interceptor { private String credentials; public BasicAuthInterceptor(String user, String password) { this.credentials = Credentials.basic(user, password); } @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Request authenticatedRequest = request.newBuilder() .header(“Authorization”, credentials).build(); … Read more

Android Unit Test with Retrofit2 and Mockito or Robolectric

It is generally not a good idea to test real server requests. See this blog post for an interesting discussion on the topic. According to the author, using your real server is a problem because: Another moving piece that can intermittently fail Requires some expertise outside of the Android domain to deploy the server and … Read more

Retrofit 2 with only form-data

Here’s another Solution using request body: RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart(“param1”, param1) .addFormDataPart(“param2”, param2) .build(); apiInterface.somePostMethod(requestBody).enqueue( //onResponse onFailure methods ); here’s my api inteface POST method @POST(“somePostMethod”) Call<ResponseBody> somePostMethod(@Body RequestBody body); Hope it helps.

How to Exponential Backoff retry on kotlin coroutines

I would suggest to write a helper higher-order function for your retry logic. You can use the following implementation for a start: suspend fun <T> retryIO( times: Int = Int.MAX_VALUE, initialDelay: Long = 100, // 0.1 second maxDelay: Long = 1000, // 1 second factor: Double = 2.0, block: suspend () -> T): T { … Read more

Unsupported operation: Android, Retrofit, OkHttp. Adding interceptor in OkHttpClient

This issue occurs when you change Retrofit 2.0-beta2 to Retrofit 2.0-beta3. You have to use builder if you want to create OkHttpClient object. Change : OkHttpClient okClient = new OkHttpClient(); okClient.interceptors().add(new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request original = chain.request(); // Request customization: add request headers Request.Builder requestBuilder = original.newBuilder() … Read more

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