okhttp3

OkHttp 3 response cache (java.net.UnknownHostException)

≯℡__Kan透↙ 提交于 2019-12-04 07:09:42
问题 I'm really stuck at this problem. I want to cache server response for some time to use the data when device is offline. So I set a cache to my OkHttpClient . Also I set Cache-Contlol header. Here is my code: public class MainActivity extends AppCompatActivity { Retrofit retrofit; RecyclerView recyclerView; RandomUserAdapter mAdapter; Cache cache; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity

Retrofit Offline cashing returns a null response.body()

雨燕双飞 提交于 2019-12-04 05:16:03
问题 I tried this link and this link to construct an offline Retrofit cache. The problem is that if I put the phone in Airplane mode, the Response.body() is always null. Here's my code: OkHttpClient client = new OkHttpClient .Builder() .cache(new Cache(App.sApp.getCacheDir(), 10 * 1024 * 1024)) // 10 MB .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); if (App.isNetworkAvailable()) { request = request

Retrofit OKHTTP Offline caching not working

拈花ヽ惹草 提交于 2019-12-03 16:36: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 + provideCacheInterceptor() as a NetworkInterceptor, but I kept getting: 504 Unsatisfiable Request (only-if-cached) and a

Not able to load data from cache okHttp & retrofit

时光怂恿深爱的人放手 提交于 2019-12-03 12:30:19
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 .addInterceptor(new Interceptor() { @Override public okhttp3.Response intercept(Chain chain) throws

Upload dynamic number of files with okHttp3

柔情痞子 提交于 2019-12-03 12:28:04
问题 How to manage upload of dynamic number of files with OkHttp v3, I have already implemented with older version of OkHttp which was compile 'com.squareup.okhttp:okhttp:2.6.0' There are some changes in class Form and Multipart bodies are now modeled. They've replaced the opaque FormEncodingBuilder with the more powerful FormBody and FormBody.Builder combo. Similarly they've upgraded MultipartBuilder into MultipartBody, MultipartBody.Part, and MultipartBody.Builder. below code is of older version

How to resolve D8 warning: D8: Type `org.conscrypt.Conscrypt` was not found

吃可爱长大的小学妹 提交于 2019-12-03 10:41:54
/Volumes/SSD/.gradle/caches/modules-2/files-2.1/com.squareup.okhttp3/okhttp/3.10.0/7ef0f1d95bf4c0b3ba30bbae25e0e562b05cf75e/okhttp-3.10.0.jar: D8: Type `org.conscrypt.Conscrypt` was not found, it is required for default or static interface methods desugaring of `okhttp3.internal.platform.Platform okhttp3.internal.platform.ConscryptPlatform.buildIfSupported()` I was able to solve the problem with adding: -dontwarn okhttp3.internal.platform.ConscryptPlatform to the proguard-rules.pro file. According to https://github.com/square/okhttp/issues/3922 , it is safe to ignore these warnings. If the

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

醉酒当歌 提交于 2019-12-03 10:32:16
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 { Response response = chain.proceed(chain.request()); // re-write response header to force use of cache

OkHttp3 Never Timeout on slow internet

对着背影说爱祢 提交于 2019-12-03 10:10:47
问题 First of all, I have read so many questions regarding my question but it never gives me the solution. Here are some of the questions which I read regarding my question. Question 1 Question 2 Qusetion 3 Question 4 Question 5 Question 6 Question 7 I also read this article regarding my question but it also never provide me the solution. Problem: I am using Okhhtp3 library in my application for Web Services. It's working fine but when the internet connection slow or unreliable connection it's

Unexpected end of stream on okhttp3

大城市里の小女人 提交于 2019-12-03 07:05:56
I had followed this link to refresh access token. While adding Authenticator to okHttp getting error of Unexpected end of stream on okhttp3 from retrofit call back onFailure method public class TokenAuthenticator implements Authenticator { AccessTokenRefreshModel accessTokenRefreshModel = null; @Override public Request authenticate(Route route, Response response) throws IOException { Call<UserLogin> call = iService.refreshAccessToken(BuildConfig.CLIENT_ID, refreshToken); UserLogin userLogin = call.execute().body(); // Add new header to rejected request and retry it return response.request()

OkHttp proxy settings

大城市里の小女人 提交于 2019-12-03 05:55:28
I have to setup a proxy to send a JSON using POST, using proxyHost and proxyPort. public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); Proxy proxyTest = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("proxy", proxyPort)); OkHttpClient client = new OkHttpClient() .proxy(proxyTest) .build(); //OkHttpClient.Builder builder = new OkHttpClient.Builder(); //builder.proxy(proxySAP); //client.setProxy(proxySAP) //OkHttpClient client = builder.build();; String post(String url, String json) throws IOException { RequestBody body = RequestBody.create(JSON, json);