Freso:我如何设置OK Http客户端?

时间:2015-10-27 23:16:16

标签: android fresco

因此,我要求的某些图片需要添加身份验证标头

我正在使用Retrofit 2.0,它有一个带有拦截器的OkHttp客户端,可以将用户令牌添加到每个请求的标头

 okHttpClient.interceptors().add(new Interceptor() {

            @Override
            public Response intercept(Chain chain) throws IOException {
                Request originalRequest = chain.request(); //Current Request
                Request requestWithToken = null; //The request with the access token which we will use if we have one instead of the original
                requestWithToken = originalRequest.newBuilder().addHeader(Constants.UrlParamConstants.HEADER_AUTHORIZATION,String.format(Constants.UrlParamConstants.HEADER_AUTHORIZATION_VALUE, MyApplication.getInstance().getUser().getApiToken())).build();
                Response response = chain.proceed((requestWithToken != null ? requestWithToken : originalRequest)); //proceed with the request and get the response
                return response;
            }
        });

我想知道如何为Fresco库设置相同的okHttp客户端实例。

我知道您需要添加此依赖项以将OkHttp与Fresco一起使用,但是如何设置客户端?

  compile "com.facebook.fresco:imagepipeline-okhttp:0.8.0+"

在一天结束时,我只需要为图像请求设置身份验证标头

感谢阅读

1 个答案:

答案 0 :(得分:3)

http://frescolib.org/docs/using-other-network-layers.html

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);