okhttp

How to add Api_KEY into interceptor using okhttp

馋奶兔 提交于 2020-02-24 06:56:02
问题 I have this service where I want to put the token as an interception in the okhttp instead of passing as a parameter with @Header("MY_API_KEY") This is my code regarding the service /** * Provides the [PHService] */ fun provideService(): PHService { val logger = HttpLoggingInterceptor() logger.level = HttpLoggingInterceptor.Level.BASIC val client = OkHttpClient.Builder() .addInterceptor(logger) .build() return Retrofit.Builder() .baseUrl(BuildConfig.API_URL) .client(client)

How to add Api_KEY into interceptor using okhttp

為{幸葍}努か 提交于 2020-02-24 06:55:00
问题 I have this service where I want to put the token as an interception in the okhttp instead of passing as a parameter with @Header("MY_API_KEY") This is my code regarding the service /** * Provides the [PHService] */ fun provideService(): PHService { val logger = HttpLoggingInterceptor() logger.level = HttpLoggingInterceptor.Level.BASIC val client = OkHttpClient.Builder() .addInterceptor(logger) .build() return Retrofit.Builder() .baseUrl(BuildConfig.API_URL) .client(client)

OkHttp 3.11 and TLS 1.2 support

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-22 07:18:25
问题 The support for TLS v1.2 was added in Android 4.2, but it wasn't enabled by default. This issue was quite easy to fix with OkHttp 3.x by providing a custom SSLSocketFactory implementation to the OkHttp client: OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setSocketFactory(new MySSLSocketFactory()); In my case the custom socket factory was setting the enabled protocols like this: private static final String[] TLS_PROTOCOLS = new String[]{ "TLSv1.1", "TLSv1.2" }; public

Android how to get response string from Callback using OkHttp?

a 夏天 提交于 2020-02-03 05:13:01
问题 This is my code: OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build(); Callback callback = new Callback() { @Override public void onFailure(Request request, IOException e) { } @Override public void onResponse(Response response) throws IOException { } }; okHttpClient.newCall(request).enqueue(callback); String responseString; In the above code, I want to store the value of response.body().string() from the

Android how to get response string from Callback using OkHttp?

萝らか妹 提交于 2020-02-03 05:12:04
问题 This is my code: OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build(); Callback callback = new Callback() { @Override public void onFailure(Request request, IOException e) { } @Override public void onResponse(Response response) throws IOException { } }; okHttpClient.newCall(request).enqueue(callback); String responseString; In the above code, I want to store the value of response.body().string() from the

OkHttpClient throws exception after upgrading to OkHttp3

本小妞迷上赌 提交于 2020-01-28 17:40:23
问题 I'm using following lines of code to add a default header to all of my requests sent using Retrofit2: private static OkHttpClient defaultHttpClient = new OkHttpClient(); static { defaultHttpClient.networkInterceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request().newBuilder() .addHeader("Accept", "Application/JSON").build(); return chain.proceed(request); } }); } After upgrading retrofit to beta-3 version, I

OkHttpClient throws exception after upgrading to OkHttp3

人盡茶涼 提交于 2020-01-28 17:40:15
问题 I'm using following lines of code to add a default header to all of my requests sent using Retrofit2: private static OkHttpClient defaultHttpClient = new OkHttpClient(); static { defaultHttpClient.networkInterceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request().newBuilder() .addHeader("Accept", "Application/JSON").build(); return chain.proceed(request); } }); } After upgrading retrofit to beta-3 version, I

Profiling http request with OkHttp

爱⌒轻易说出口 提交于 2020-01-24 06:48:15
问题 How to track detailed request time with OkHttp. I want to get: connection time; sending time; receiving time; I tried to use Interceptors mechanism, but it provides only total request time. class LoggingInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); long t1 = System.nanoTime(); logger.info(String.format("Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers())); Response

Profiling http request with OkHttp

有些话、适合烂在心里 提交于 2020-01-24 06:46:49
问题 How to track detailed request time with OkHttp. I want to get: connection time; sending time; receiving time; I tried to use Interceptors mechanism, but it provides only total request time. class LoggingInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); long t1 = System.nanoTime(); logger.info(String.format("Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers())); Response

Callback failure for canceled call in OkHttp

喜夏-厌秋 提交于 2020-01-23 12:25:13
问题 Canceling enqueued calls in OkHttp (version 2.4) client sometimes throws Callback failure for canceled call to http://... and neither of callback methods gets executed. Since I have to provide either positive or negative feedback to the user, having callback eaten up like that is not acceptable. How can I get around that error? I have created test application with two buttons (one for starting download request, another for canceling it), text view and image view, that shows error most of the