By default, the image pipeline uses the HttpURLConnection networking library bundled with Android. Apps may have their own network layer they may wish to use instead.

Using OkHttp

OkHttp is a popular open-source networking library. The image pipeline has a backend that uses OkHttp instead of the Android default.

OkHttp in Gradle

In order to use it, the dependencies section of your build.gradle file needs to be changed. Along with the Gradle dependencies given on the download page, add just one of these:

For OkHttp2:

1
2
3
4
dependencies {
  // your project's other dependencies
  compile "com.facebook.fresco:imagepipeline-okhttp:1.0.1"
}

For OkHttp3:

1
2
3
4
dependencies {
  // your project's other dependencies
  compile "com.facebook.fresco:imagepipeline-okhttp3:1.0.1"
}

OkHttp in Eclipse

Eclipse users should depend on either the imagepipeline-okhttp and imagepipeline-okhttp3 directories in the frescolib tree as described in the Eclipse instructions.

Configuring the image pipeline with OkHttp

You must also configure the image pipeline a little differently. Instead of using ImagePipelineConfig.newBuilder, use OkHttpImagePipelineConfigFactory instead:

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

Handling sessions and cookies correctly

The OkHttpClient you pass to Fresco in the above step should be set up with interceptors needed to handle authentications to your servers. See this bug and the solutions outlined there for some problems that have occurred with cookies.

Using your own network fetcher (optional)

For complete control on how the networking layer should behave, you can provide one for your app. You must subclass NetworkFetcher, which controls communications to the network. You can also optionally subclass FetchState, which is a data structure for request-specific information.

Our implementation for OkHttp 3 can be used as an example. See its source code.

You must pass your network producer to the image pipeline when configuring it:

1
2
3
4
5
ImagePipelineConfig config = ImagePipelineConfig.newBuilder()
  .setNetworkFetcher(myNetworkFetcher);
  . // other setters
  .build();
Fresco.initialize(context, config);