Search code examples
androidfresco

Freso: How can I set the OK Http client?


So some images I request require an authentication header to be added

I am using Retrofit 2.0 which has this OkHttp client with a interceptor to add the user token to the header to every request

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

I would like to know how can I set the same okHttp client instance for Fresco library.

I am aware that you need to add this dependency to use OkHttp with Fresco but how about setting the client?

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

At the end of the day I just need to set authentication header for an image request

thanks for reading


Solution

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