okhttp

HTTP Caching with Retrofit 2.0.x

纵然是瞬间 提交于 2019-12-02 18:18:29
I'm trying to cache some responses in my app using Retrofit 2.0, but I'm missing something. I installed a caching file as follows: private static File httpCacheDir; private static Cache cache; try { httpCacheDir = new File(getApplicationContext().getCacheDir(), "http"); httpCacheDir.setReadable(true); long httpCacheSize = 10 * 1024 * 1024; // 10 MiB HttpResponseCache.install(httpCacheDir, httpCacheSize); cache = new Cache(httpCacheDir, httpCacheSize); Log.i("HTTP Caching", "HTTP response cache installation success"); } catch (IOException e) { Log.i("HTTP Caching", "HTTP response cache

How to get response body to retrofit exception?

随声附和 提交于 2019-12-02 17:54:53
I am trying to connect to rest service via retrofit in android application. I am getting responses. But when there is some error response from the service, conversion exception occurs and now I want to do some actions based on the response body. But I am getting response body as NULL. But retrofit log has a error message. Why is this happening. D/Reftofit log(24856): OkHttp-Received-Millis: 1397527055676 D/Reftofit log(24856): OkHttp-Response-Source: NETWORK 200 D/Reftofit log(24856): OkHttp-Sent-Millis: 1397527055492 D/Reftofit log(24856): Server: Apache/2.2.22 (Ubuntu) D/Reftofit log(24856):

Android OkHttp, refresh expired token

陌路散爱 提交于 2019-12-02 17:33:24
Scenario : I am using OkHttp / Retrofit to access a web service: multiple HTTP requests are sent out at the same time. At some point the auth token expires, and multiple requests will get a 401 response. Issue : In my first implementation I use an interceptor (here simplified) and each thread tries to refresh the token. This leads to a mess. public class SignedRequestInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); // 1. sign this request request = request.newBuilder() .header(AUTH_HEADER_KEY, BEARER

Download progress with RxJava, OkHttp and Okio in Android

谁都会走 提交于 2019-12-02 17:17:32
In our app I download an image file with this code. I need to show download progress( downloaded bytes in percentage ) on UI. How I can get download progress in this code? I searched for solution, but still can't manage to do it on my own. Observable<String> downloadObservable = Observable.create( sub -> { Request request = new Request.Builder() .url(media.getMediaUrl()) .build(); Response response = null; try { response = http_client.newCall(request).execute(); if (response.isSuccessful()) { Log.d(TAG, "response.isSuccessful()"); String mimeType = MimeTypeMap.getFileExtensionFromUrl(media

How to specify a default user agent for okhttp 2.x requests

喜你入骨 提交于 2019-12-02 17:02:12
I am using okhttp 2.0 in my Android app and didn't find a way to set some common User Agent for all outgoing requests. I thought I could do something like OkHttpClient client = new OkHttpClient(); client.setDefaultUserAgent(...) ...but there's no such method or similar. Of course I could provide some extension utility method which would wrap a RequestBuilder to attach .header("UserAgent") and then I would use it for building all my requests, but I thought maybe I missed some existing and simpler way? You can use an interceptor to add the User-Agent header to all your requests. For more

spring服务方式配置okhttp3

浪子不回头ぞ 提交于 2019-12-02 07:17:32
问题 如果把OKhttp以Spring服务方式配置,就解决了从配置中心运行时刷新配置参数的问题。 OkHttpConfig.java package com.zyl.config; import okhttp3.OkHttpClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class OkHttpConfig { @Bean public OkHttpClient.Builder builder(){ return new OkHttpClient.Builder(); } @Bean public ConnectionPool connectionPool(){ return new ConnectionPool(); } } 配置OKhttp的构建对象。 IOkHttpService.java package com.zyl.service; import okhttp3.OkHttpClient; public interface IOkHttpService { OkHttpClient okHttpClient(); String

spring注解配置okhttp3

我与影子孤独终老i 提交于 2019-12-02 07:17:24
背景 之前在spring上面使用过okhttp: spring传统xml配置okhttp3 Component package com.zyl.config; import okhttp3.ConnectionPool; import okhttp3.Credentials; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; /** okhttp3 configuration */ @Component public class OkHttpConfiguration { private Logger logger = LoggerFactory.getLogger

Retrofit offline request and response

前提是你 提交于 2019-12-02 04:43:06
问题 I've already read many questions and answers about my issue, but I slill can't understand how to solve it. I need to fetch response from server and store it in cache. After that when device is offline I want to use cached response. When device is online I want to fetch response exactly from server. Looks not so complicated. Here is the way (samples of code) I try to do this: 1)Method to create a Cache Cache provideOkHttpCache() { int cacheSize = 10 * 1024 * 1024; // 10 MiB Cache cache = new

Exclude default headers set by OkHttp library

点点圈 提交于 2019-12-02 04:17:21
问题 In recent versions of OkHttp library, headers like "Accept-Encoding" and "User-Agent" are added automatically if you don't provide them by yourself. Is there a way to disable this feature? 回答1: Strip 'em with a Network Interceptor. client.networkInterceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request() .newBuilder() .removeHeader("Accept-Encoding") .removeHeader("User-Agent") .build(); return chain.proceed

How to provide Context with Dagger2

你离开我真会死。 提交于 2019-12-02 03:12:14
I am learning Android and I am following some guides for Retrofit2 with RxJava and Dagger2. Now I want to handle no internet connection case. I've found this answer , which seems to be elegant, but I do not understand how to apply it. I've got some NetworkModule , with OkHttpClient provider. I assume I need to create OkHttpClient.Builder with interceptor. So it should look something like this: ` @Provides @Singleton OkHttpClient provideOkHttpClient(Cache cache) { ConnectivityInterceptor ci = new ConnectivityInterceptor(networkObservable())); OkHttpClient.Builder.addInterceptor(ci) return