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”, “Jurassic@Park.com”) .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();

Glide – adding header to request

Since 3.6.0 it’s possible to set custom headers for each request: GlideUrl glideUrl = new GlideUrl(“url”, new LazyHeaders.Builder() .addHeader(“key1”, “value”) .addHeader(“key2”, new LazyHeaderFactory() { @Override public String buildHeader() { String expensiveAuthHeader = computeExpensiveAuthHeader(); return expensiveAuthHeader; } }) .build()); Glide….load(glideUrl)….;

Is it thread-safe to make calls to OkHttpClient in parallel?

You asked a very good question, allow me to expound holistically. Based on GitBub issues opened for OkHttp, my summary is this. Question I believe a single instance of OkHttpClient should be reused to create multiple connections. Is OkHttpClientthread safe? If not, isOkHttpClient.open()` thread safe? Answer Yes. It is designed to be treated as a … Read more

Retrofit – Intercept responses globally

I was able to accomplish that by adding an interceptor to the OkHttpClient that retrofit is using. Kotlin + Retrofit 2.x val clientBuilder = OkHttpClient.Builder() clientBuilder.addInterceptor { chain -> val request = chain.request() val response = chain.proceed(request) if (response.code() == 403) { handleForbiddenResponse() } response } Retrofit 2.x: OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); clientBuilder. addInterceptor(new … Read more

Okhttp refresh expired token when multiple requests are sent to the server

You can do this: Add those as data members: // these two static variables serve for the pattern to refresh a token private final static ConditionVariable LOCK = new ConditionVariable(true); private static final AtomicBoolean mIsRefreshing = new AtomicBoolean(false); and then on the intercept method: @Override public Response intercept(@NonNull Chain chain) throws IOException { Request request … Read more

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