自定义网络加载
Image pipeline 默认使用HttpURLConnection。应用可以根据自己需求使用不同的网络库。
OkHttp
OkHttp 是一个流行的开源网络请求库。Image pipeline有一个使用OkHttp替换掉了Android默认的网络请求的补充。
如果需要使用OkHttp, 不要使用这个下载页面的gradle依赖配置,应该使用下面的依赖配置
For OkHttp2:
1 2 3 4 | dependencies { // your project's other dependencies compile "com.facebook.fresco:imagepipeline-okhttp:0.12.0+" } |
For OkHttp3:
1 2 3 4 | dependencies { // your project's other dependencies compile "com.facebook.fresco:imagepipeline-okhttp3:0.12.0+" } |
Eclipse 中使用 OkHttp
Eclipse 用户需要依赖frescolib
目录下的imagepipeline-okhttp
或 imagepipeline-okhttp3
。 参考在Eclipse中使用Fresco.
配置 image pipeline
配置Image
pipeline这时也有一些不同,不再使用ImagePipelineConfig.newBuilder
,而是使用OkHttpImagePipelineConfigFactory
:
1 2 3 4 5 6 7 8 | Context context; OkHttpClient okHttpClient; // build on your own ImagePipelineConfig config = OkHttpImagePipelineConfigFactory .newBuilder(context, okHttpClient) . // other setters . // setNetworkFetcher is already called for you .build(); Fresco.initialize(context, config); |
处理 Session 和 Cookie
你传给OkHttpClient
需要处理服务器的安全校验工作(可以通过Interceptor处理)。参考这个bug 来处理自定义网络库可能发生的 Cookie 相关的问题。
使用自定的网络层
为了完全控制网络层的行为,你可以自定义网络层。继承NetworkFetchProducer, 这个类包含了网络通信。
你也可以选择性地继承FetchState, 这个类是请求时的数据结构描述。
默认的 OkHttp 3
可以作为一个参考. 源码在这 its source code..
在配置Image pipeline时,把producer传递给Image pipeline。
1 2 3 4 5 | ImagePipelineConfig config = ImagePipelineConfig.newBuilder() .setNetworkFetcher(myNetworkFetcher); . // other setters .build(); Fresco.initialize(context, config); |