okhttp3

Caching with Retrofit 2.0 and okhttp3

左心房为你撑大大i 提交于 2019-12-04 19:39:54
I'm trying to implement caching using Retrofit and OkHttp. Here what I've already done: private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request originalRequest = chain.request(); Request.Builder request = originalRequest.newBuilder(); Response response = chain.proceed(request.build()); return response.newBuilder() .removeHeader("Pragma") .removeHeader("Cache-Control") .header("Cache-Control", "max-age=2419200") .build(); } }; @Provides @Singleton OkHttpClient

OKHttp Authenticator custom http code other than 401 and 407

大憨熊 提交于 2019-12-04 19:08:17
I have oauth token implemented on server side but upon Invalid token or Token expirey i am getting 200 http status code but in response body i have {"code":"4XX", "data":{"some":"object"} When i try to read string in interceptor i get okhttp dispatcher java.lang.illegalstateexception closed because response.body().string() must be called only once. Also i read from here Refreshing OAuth token using Retrofit without modifying all calls that we can use OkHttp Authenticator class but it works only with 401/407 i havent triedn as i will not get this. Is there any way we can customize Authenticator

Not able to load data from cache okHttp & retrofit

拥有回忆 提交于 2019-12-04 17:41:54
问题 Here is my code where i am calling api and also define cache for okhttp with retrofit: public class DemoPresenter { DemoView vDemoView; private Context mContext; public DemoPresenter(Context mcontext, DemoView vDemoView) { this.vDemoView = vDemoView; this.mContext = mcontext; } public void callAllProduct() { if (vDemoView != null) { vDemoView.showProductProgressBar(); } OkHttpClient okHttpClient = new OkHttpClient.Builder() .cache(new Cache(mContext.getCacheDir(), 10 * 1024 * 1024)) // 10 MB

Got this error with retrofit2 & OkHttp3. Unable to resolve host “<host-name>”: No address associated with hostname

我的未来我决定 提交于 2019-12-04 16:32:10
问题 I am using the retrofit 2 and OkHttp3 to request data from server. I just added a offline cache code but It's not working as expected. I got the error "Unable to resolve host "<>": No address associated with hostname." This occurs when It's try to get the retrieve data from the cache(when no internet connection). A code snippet is below. public static Interceptor provideCacheInterceptor() { return new Interceptor() { @Override public Response intercept(Chain chain) throws IOException {

Retrofit OKHTTP Offline caching not working

梦想的初衷 提交于 2019-12-04 13:38:39
问题 I read dozens of tutorial and Stackoverflow answers to my problem but nothing is working for me! Also, most of them are old so probably OKHTTP changed somehow. All I want is to enable offline caching for Retrofit. I am using GET I tried using only offlineCacheInterceptor as an Interceptor, but I kept getting: Unable to resolve host "jsonplaceholder.typicode.com": No address associated with hostname I tried using a combination of offlineCacheInterceptor as an Interceptor +

Enable TLS 1.2 in Android 4.4

拜拜、爱过 提交于 2019-12-04 12:17:43
问题 I use Retrofit and OkHttp3 for making requests. I konw that in Android 4.4 TLS 1.1 and TLS 1.2 are not enabled by defult. So i'm trying to enable them. But so far i had no sucsess. I read that it could be a problem of the android studio emulator, but i can't make a test on a real device with andoroid 4.4 rigthnow This is what i have done so far: private <S> S createService(Class<S> serviceClass) { Retrofit retrofit = builder.client(getNewHttpClient()).build(); return retrofit.create

Retrofit 2.0 multiple interceptors

╄→гoц情女王★ 提交于 2019-12-04 11:45:21
I am working with retrofit and need to be able to use multiple interceptors. Currently I am using one to automatically append an auth token but i need to be able to make calls with no auth token. If i add another interceptor with no auth token in the header how do I use that one instead of the auth token interceptor. val interceptor: Interceptor = Interceptor { chain -> val newRequest = chain.request().newBuilder(). addHeader("Auth_Token", pref.getString(PSPreferences.prefAuthKey, "")). cacheControl(CacheControl.FORCE_NETWORK). build() chain.proceed(newRequest) } okHttpClient = OkHttpClient

java.net.SocketTimeoutException: timeout

醉酒当歌 提交于 2019-12-04 08:50:14
问题 With OkHttp library, application is facing following SocketTimeoutException issue. If request size is less, then it is working fine(Less than 1MB). I am getting this exception within 10 seconds, even my socket timeout( readTimeout ) value is much higher. It is consistently failing for a request(Size is 1.8MB). When I executed a request with HttpUrlConnection it is working fine. What could be a possible reason of failure? 03-29 12:16:38.997 32066-4018/com.mobile W/System.err: java.net

How to add Basic Authentication in Picasso 2.5.2 with OkHttp 3.2.0

和自甴很熟 提交于 2019-12-04 08:06:26
I am using the picasso 2.5.2 library to download the bitmap so in the api I need to pass the Basic Authentication in the headers. i have tried the following SO ansers but none of them work with the latest picasso and OkHttp libraries. Answer - 1 Answer - 2 Answer - 3 Thanks in advance. Try configuring an OkHttp3 client with authenticator, depending on your scheme and situation: OkHttpClient okHttpClient = new OkHttpClient.Builder() .authenticator(new Authenticator() { @Override public Request authenticate(Route route, Response response) throws IOException { String credential = Credentials

Send authorization header with every request in webview using okhttp in android

我的梦境 提交于 2019-12-04 08:03:07
I am using WebView to display web page, but the server expects an authorization token with every request from my webview. Anybody know if this is possible ? I referred this post in #SO adding-header-to-all-request-with-retrofit-2 . But I'm not able to get the result. This is my code (bare with my coding standard, I'm a beginner) public class TableViewTest extends AppCompatActivity { ScrollView scrollView; WebView webView; SharedPreferences pref; @SuppressLint("SetJavaScriptEnabled") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);