okhttp

HTTP static resources caching forever stategy

痞子三分冷 提交于 2019-12-08 16:31:37
问题 In Effective Android HTTP Jesse Wilson mentioned the following: Serving static resources like images? Use a permanent URL and let it cache forever I am using Picasso with a specific OkHttp instance for fetching and caching images. I am wondering how to setup the let it cache forever strategy? I can only think about overriding the HTTP cache control header max-stale ( Cache-Control: max-stale= )with the highest possible value, is there another (better) way? 回答1: Best way is to configure your

Flush previous Retrofit responses

六月ゝ 毕业季﹏ 提交于 2019-12-08 14:20:12
问题 In my Android app I need to download metadata for some files from a XML API. I am using RetroFit to handle the requests that I've implemented as asynchronous ones, in the Callback I parse the XML and save in a file the data I need. When I need to get info for many files, I get OutOfMemoryError and analyzing the heap with MAT, I discovered that XML body of previous responses is still there. Is there any way to force the app to flush the already used XML? 回答1: I think I found a solution: By

Caching Images and strings using Retrofit, okhttp, picasso

孤者浪人 提交于 2019-12-08 09:03:33
问题 I am working on an app with a lot of dynamic and changing content. I pull all my data from my server when the app is loading. As a result, nearly every activity/fragment is loading separately which will cause the user to wait a lot of time for each "page" to load individually. My goal is to create one loading page when the app starts while being responssible for all the downloading and will disk cache all the images and info(strings) and ti pull them at the right time. (or at least to most of

Caching with OkHttp (without Retrofit)

杀马特。学长 韩版系。学妹 提交于 2019-12-08 08:36:38
问题 In my Application onCreate, I'm creating a 10MB cache: try { File httpCacheDir = new File(getApplicationContext().getCacheDir(), Constants.AppName); long httpCacheSize = 10 * 1024 * 1024; // 10 MiB HttpResponseCache.install(httpCacheDir, httpCacheSize); } catch (IOException ex) { Log.i(Constants.AppName, "HTTP response cache installation failed: " + ex); } And my call to a resource: OkHttpClient client = new OkHttpClient(); client.setResponseCache(HttpResponseCache.getInstalled());

OkHttp, Android - download a html page and display this content in a view

纵饮孤独 提交于 2019-12-08 02:29:21
问题 I would like download a html page in order to display some information in a TextView. Everytime, my program displays "error" in the TextView. Why doesn't my program work ? I add in the AndroidManifest.xml file, this line: <uses-permission android:name="android.permission.INTERNET" /> Here my activity_main.xml file: <?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout

OkHttp离线缓存实现

╄→尐↘猪︶ㄣ 提交于 2019-12-07 20:33:28
场景应用: OkHttp内部已经支持了标准的Http协议缓存策略,如Last-Modified, Etag, Cache-Control等方式,看起来已经是非常够用了,但是我们还想让缓存在这样的场景下得到使用: 当客户端设备网络中断或者服务端出现了错误,但是本地存在缓存副本的时候,我们依然想取用这部分缓存。 OkHttp保存缓存的时机: okhttp3.internal.http.HttpEngine: private void maybeCache() throws IOException { InternalCache responseCache = Internal.instance.internalCache(client); if (responseCache == null) return; // Should we cache this response for this request? if (!CacheStrategy.isCacheable(userResponse, networkRequest)) { if (HttpMethod.invalidatesCache(networkRequest.method())) { try { responseCache.remove(networkRequest); } catch (IOException

java.lang.NoClassDefFoundError: com.squareup.okhttp.MediaType

半腔热情 提交于 2019-12-07 18:33:33
问题 I am getting this error while changing my code from default HTTPClient to OkHTTP . Searched all through the web but it is not getting resolved. I tried the same with OkHTTP library from here and also with dependency but still the error is same. Please help me to resolve this. The Errors are: 01-01 22:03:18.038: E/AndroidRuntime(1460): java.lang.RuntimeException: An error occured while executing doInBackground() 01-01 22:03:18.038: E/AndroidRuntime(1460): at android.os.AsyncTask$3.done

Retrofit源码解析一——从create方法切入

試著忘記壹切 提交于 2019-12-07 12:09:41
Retrofit源码解析 OKHTTP的基本认识 创建OkHttpClient,OkHttpClient负责实际请求的发送,并且可以对他配置超时时间、网络代理、缓存等。 创建RequestHeader和RequestBody。 创建Request。 由OkHttpClient发送request获取response。 retrofit 基本使用 准备一个接口serverApi,里面写好各种实现方法,加好GET/POST注解。 创建Retrofit对象。 用retrofit调用create方法创建serverApi的实现类。 通过调用实现类的各种方法进行接口调用。 retrofit的思想 接口隔离。 方便调用,免除了层层创建对象的麻烦。 复用OkHttpClient。 方便json解析出入参数。 方便用rxjava进行函数式编程。 统一了BaseUrl。 retrofit的不足 有点死板,不支持返回中无responseBody的情况。 不支持WebSocket,而Okhttp实际上是支持的。 retrofit原理解析 create(final Class<T> service)方法 public <T> T create(final Class<T> service) { //对传入的service进行检查,首先必须是接口,并且不能继承其他接口。 Utils

2019最新Android面试题

丶灬走出姿态 提交于 2019-12-07 10:40:49
原文链接: https://blog.csdn.net/wen_haha/article/details/88362469 版权声明:本文为博主原创文章,转载请附上博文链接! 前言 金三银四到来了,找工作的好时候到了,小伙伴们是不是都在忙着找工作呢,小弟前一阵也是忙着在找工作,面试了好多公司,所幸的是进到了自己心仪的公司,也是很幸运的。下面我将自己亲身实战的面试题及收到的面试题总结并分享答案出来。欢迎各位大哥指导、指点。 下面这些只是Android方面的知识,如果有需要Java方面的面试题的话,可以在下面留言。 1.Activity生命周期(这个是必问的) onCreate() 创建活动,做一些数据初始化操作 onStart() 由不可见变为可见 onResume() 可以与用户进行交互,位于栈顶 onPause() 暂停,启动或恢复另一个活动时调用 onStop() 停止,变为不可见 onDestroy() 销毁 onRestart() 由停止状态变为运行状态 2.Fragment生命周期 (这个有可能会问到,问的机率不是很大) 3.Service生命周期 onCreate() 首次创建服务时,系统将调用此方法。如果服务已在运行,则不会调用此方法,该方法只调用一次。 onStartCommand() 当另一个组件通过调用startService()请求启动服务时,系统将调用此方法

Volley + OkHttp on Android gives error on status 200 response

假如想象 提交于 2019-12-07 05:04:49
问题 When I make my request with only Volley everything goes well and my StringRequest goes to onResponse. But when I switch to Volley + Okhttp combination, my request goes through, I receive the same response as before but then I get the following error message: E/Volley﹕ [122319] BasicNetwork.performRequest: Unexpected response code 200 for <my request url> java.io.IOException: closed com.android.volley.NetworkError: java.io.IOException: closed at com.android.volley.toolbox.BasicNetwork