okhttp

Intercept and retry call by means of OkHttp Interceptors

主宰稳场 提交于 2019-12-03 06:34:39
问题 I need to retry request inside of OkHttp Interceptor . For example there is incoming request which needs Authorization token. If Authorization token is expired, server returns response with 403 code. In this case I am retrieving a new token and trying to make call again by using the same chain object. But OkHttp throws an exception, which states that you cannot make two requests with the same chain object. java.lang.IllegalStateException: network interceptor org.app.api.modules

How to handle pagination in retrofit

穿精又带淫゛_ 提交于 2019-12-03 06:15:16
I'm building an app using retrofit. Everything's working swimmingly, but I'm worried about the size of my API requests and would like to split them up using pagination. What would be the best strategy to page through the API automatically with Retrofit, so that all available data is downloaded by default? First, the pagination would need to be supported by the backend service that you're using. Second if you're looking to get an example of how this can be implemented from the client side using retrofit I would recommend you take a look at the u2020 project from @JakeWharton. The GalleryService

Detect if OkHttp response comes from cache (with Retrofit)

天大地大妈咪最大 提交于 2019-12-03 05:20:44
Is there a way to detect if a Retrofit response comes from the configured OkHttp cache or is a live response? Client definition: Cache cache = new Cache(getCacheDirectory(context), 1024 * 1024 * 10); OkHttpClient okHttpClient = new OkHttpClient.Builder() .cache(cache) .build(); Api definition: @GET("/object") Observable<Result<SomeObject>> getSomeObject(); Example call: RetroApi retroApi = new Retrofit.Builder() .client(okHttpClient) .baseUrl(baseUrl) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build() .create(RetroApi.class);

Now that SSLSocketFactory is deprecated on Android, what would be the best way to handle Client Certificate Authentication?

╄→尐↘猪︶ㄣ 提交于 2019-12-03 05:06:00
问题 I am working on an Android app that requires Client Certificate Authentication (with PKCS 12 files). Following the deprecation of all that's apache.http.* , we have started a pretty big work of refactoring on our network layer, and we have decided to go with OkHttp as a replacement, and so far I like that very much. However, I haven't found any other way to handle client certificate auth without using SSLSocketFactory , with OkHttp or anything else for that matter. So what would be the best

HTTP Caching with Retrofit 2.0.x

*爱你&永不变心* 提交于 2019-12-03 05:04:46
问题 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

How to change body in OkHttp Response?

亡梦爱人 提交于 2019-12-03 05:02:48
问题 I'm using retrofit. To catch response i'm using Interceptor: OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.interceptors().add(myinterceptor); here is code of interceptor: new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Response response = chain.proceed(request); if (path.equals("/user")){ String stringJson = response.body().string(); JSONObject jsonObject = new JSONObject(stringJson); jsonObject.put(

How Retrofit with OKHttp use cache data when offline

╄→尐↘猪︶ㄣ 提交于 2019-12-03 04:44:28
问题 I want to Retrofit with OkHttp uses cache when is no Internet. I prepare OkHttpClient like this: RestAdapter.Builder builder= new RestAdapter.Builder() .setRequestInterceptor(new RequestInterceptor() { @Override public void intercept(RequestFacade request) { request.addHeader("Accept", "application/json;versions=1"); if (MyApplicationUtils.isNetworkAvaliable(context)) { int maxAge = 60; // read from cache for 1 minute request.addHeader("Cache-Control", "public, max-age=" + maxAge); } else {

OkHttp + Picasso + Retrofit

时光毁灭记忆、已成空白 提交于 2019-12-03 03:33:35
The question is how to combine all these 3 libraries in one project? Make one OkHttpClient to be a background layer for both Picasso and Retrofit. How to make Priority changes like in Volley lib. (for pagination)? In a nutshell: OkHttpClient okHttpClient = new OkHttpClient(); RestAdapter restAdapter = new RestAdapter.Builder().setClient(new OkClient(okHttpClient)).build(); OkHttpDownloader downloader = new OkHttpDownloader(okHttpClient); Picasso picasso = new Picasso.Builder(this).downloader(downloader).build(); I do not think it's possible to have priorities with the current version of

Android https 证书信任问题

北战南征 提交于 2019-12-03 03:28:54
因为最近公司的open api服务器访问协议换成了https,所以 android 在使用okhttp 走https 访问 的时候遇到了证书信任的问题, 在这里把我走过的弯路记下来,一如既往的话不多说,上码: OkHttpClient sClient = new OkHttpClient(); // 设置超时时间 sClient.setConnectTimeout(8000, TimeUnit.MILLISECONDS); sClient.setReadTimeout(8000, TimeUnit.MILLISECONDS); // 注册拦截器 sClient.interceptors().add(new BaseInterceptor(context)); 第一种方式: sClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 运行结果: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. 11-26 11:17:57.264 17106-17268/com.dooioo

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

时光怂恿深爱的人放手 提交于 2019-12-03 03:01:02
问题 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