How to add headers to OkHttp request interceptor?

Finally, I added the headers this way: @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request request = chain.request(); Request newRequest; newRequest = request.newBuilder() .addHeader(HeadersContract.HEADER_AUTHONRIZATION, O_AUTH_AUTHENTICATION) .addHeader(HeadersContract.HEADER_X_CLIENT_ID, CLIENT_ID) .build(); return chain.proceed(newRequest); }

Trusting all certificates with okHttp

Just in case anyone falls here, the (only) solution that worked for me is creating the OkHttpClient like explained here. Here is the code: private static OkHttpClient getUnsafeOkHttpClient() { try { // Create a trust manager that does not validate certificate chains final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public void … Read more

Can Retrofit with OKHttp use cache data when offline

Edit for Retrofit 2.x: OkHttp Interceptor is the right way to access cache when offline: 1) Create Interceptor: private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Response originalResponse = chain.proceed(chain.request()); if (Utils.isNetworkAvailable(context)) { int maxAge = 60; // read from cache for 1 minute return originalResponse.newBuilder() … Read more

How to set connection timeout with OkHttp

As of OkHttp3 you can do this through the Builder like so client = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .build(); You can also view the recipe here. For older versions, you simply have to do this OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(15, TimeUnit.SECONDS); // connect timeout client.setReadTimeout(15, TimeUnit.SECONDS); // socket timeout Request … Read more

How to make “inappropriate blocking method call” appropriate?

The warning is about methods that block current thread and coroutine cannot be properly suspended. This way, you lose all benefits of coroutines and downgrade to one job per thread again. Each case should be handled in a different way. For suspendable http calls you can use ktor http client. But sometimes there is no … 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

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