How to retry HTTP requests with OkHttp/Retrofit?

For Retrofit 2.x; You can use Call.clone() method to clone request and execute it. For Retrofit 1.x; You can use Interceptors. Create a custom interceptor OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); client.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); client.interceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); // try the request Response response … Read more

How can I pin a certificate with Square OKHTTP?

UPDATE FOR OKHTTP 3.0 OKHTTP 3.0 has built-in support for pinning certificates. Start off by pasting the following code: String hostname = “yourdomain.com”; CertificatePinner certificatePinner = new CertificatePinner.Builder() .add(hostname, “sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=”) .build(); OkHttpClient client = OkHttpClient.Builder() .certificatePinner(certificatePinner) .build(); Request request = new Request.Builder() .url(“https://” + hostname) .build(); client.newCall(request).execute(); This will fail because AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA is not a … Read more

How to enable TLSv1.3 for OkHttp 3.12.x on Android 8/9?

As shown in official link, TLSv1.3 is supported from Android 10(Api Level 29) on wards. So to support TLSv1.3 in previous versions we can integrate the conscrypt library. Conscrypt security provider includes a public API for TLS functionality. For that we have to add the dependency, dependencies { implementation ‘org.conscrypt:conscrypt-android:2.2.1’ } Here also we need … Read more

How to add parameters to api (http post) using okhttp library in Android

For OkHttp 3.x, FormEncodingBuilder was removed, use FormBody.Builder instead RequestBody formBody = new FormBody.Builder() .add(“email”, “[email protected]”) .add(“tel”, “90301171XX”) .build(); OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(url) .post(formBody) .build(); Response response = client.newCall(request).execute(); return response.body().string();

How to use OKHTTP to make a post request?

As per the docs, OkHttp version 3 replaced FormEncodingBuilder with FormBody and FormBody.Builder(), so the old examples won’t work anymore. Form and Multipart bodies are now modeled. We’ve replaced the opaque FormEncodingBuilder with the more powerful FormBody and FormBody.Builder combo. Similarly we’ve upgraded MultipartBuilder into MultipartBody, MultipartBody.Part, and MultipartBody.Builder. So if you’re using OkHttp 3.x … Read more

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); }