Blog

third party network library

How-To: Using MAP SDK with Third-Party Network Libraries

September 11, 2018 · by Rahul Sharma ·
Categories:

The Mobile Application Performance software development kit (MAP SDK) helps you create seamless mobile experiences when connection speeds are slow, or even unavailable. MAP SDK supports the most commonly used, third-party network libraries including:

  • HttpUrlConnection
  • OkHttpClient
  • Retrofit
  • Volley

In this blog post, I’ll walk you through the steps required to pass app network traffic through MAP SDK with each of these network libraries.

Note 1: All the interceptor/wrappers mentioned below are bundled with MAP SDK and present in the vocaccelerator > wrappers folder. You can include them in your project according to the library you’re using.

Note 2:  Please disable disk-level caching if you’re working with any of the HttpClients such as OkhttpClient, Volley, or Retrofit because this could lead to duplication of objects in disk cache if universal caching is enabled on MAP SDK.

HttpUrlConnection

MAP SDK by default intercepts traffic going from HttpUrlConnection. Hence, for this scenario you don’t need to use any wrapper for intercept traffic going from HttpUrlConnection. MAP SDK will automatically intercept the traffic and provide desired feature functionality.

OkHttpClient

OkHttpClient is commonly used as a networking library in Android to get data from a server. MAP SDK supports OkHttpClient and provide desired feature functionality. The integration of MAP SDK and OkHttpClient is simple and straightforward. MAP SDK has its own AkaOkHttpInterceptor for OkHttpClient to intercept network traffic. You simply need to add AkaOkHttpInterceptor as an interceptor in your OkHttpClient to intercept network traffic by MAP SDK.

Note: If you have more than one interceptor for your app make sure you add AkaOkHttpInterceptor at the end.

Sample Code:

OkHttpClient client = new OkHttpClient.Builder()

  .addInterceptor(.....) // (App interceptors if you have any depending on your use case)

  .addInterceptor(new AkaOkHttpInterceptor())

  .build();

Retrofit

Retrofit is another networking library used in Android to fetch data from a server. MAP SDK supports Retrofit and provides desired feature functionality. The integration of MAP SDK and Retrofit depends on which Retrofit version you’re using. Here are the steps for both Version 1 and Version 2:

Version 1

For Retrofit Version 1, you need to add AkaRetrofitClient as a client while building a RetrofitAdapter object.

Sample code:

RestAdapter.Builder builder = new RestAdapter.Builder()

              .setEndpoint(<END-POINT>)

              .setClient(new AkaRetrofitClient(this));

Version 2

Retrofit Version 2 by default uses OkHttpClient as networking client to get data from a server. MAP SDK has its own AkaOkHttpInterceptor for OkHttpClient to intercept network traffic. You simply need to add AkaOkHttpInterceptor as an interceptor in your OkHttpClient to intercept network traffic by MAP SDK.

Note: If you have more than one interceptor for your app make sure you add AkaOkHttpInterceptor at the end.

Sample Code:

OkHttpClient client = new OkHttpClient.Builder()

  .addInterceptor(.....) // (App interceptors if you have any depending on your use case)

  .addInterceptor(new AkaOkHttpInterceptor())

  .build();

 

final Retrofit retrofit = new Retrofit.Builder()

              .baseUrl(<Base-URL>)

              .client(client)

              .build();

Volley

Volley serves as a networking library in Android to fetch data from a server. MAP SDK supports Volley and provides desired feature functionality. The integration of MAP SDK and Volley depends on the client used with Volley to connect to network (i.e., default or HurlStack or OkHttp).

Default Volley or Volley with HurlStack

By default, Volley and HurlStack internally use HttpUrlConnection as a networking client. MAP SDK by default intercepts traffic going from HttpUrlConnection. Hence, for this scenario you don’t need to use any wrapper for intercepting image traffic. MAP SDK will automatically intercept the traffic and provide desired feature functionality.

Volley with OkHttp

When OkHttpClient is used as the networking client with Volley, MAP SDK will not intercept network traffic automatically. MAP SDK has its own AkaOkHttpInterceptor for OkHttpClient to intercept network traffic, so you’ll need to add AkaOkHttpInterceptor in your OkHttpClient to intercept network traffic with MAP SDK.

Note: If you have more than one interceptor for your app make sure you add AkaOkHttpInterceptor at the end.

Sample code:

OkHttpClient client = new OkHttpClient.Builder()

  .addInterceptor(.....) // App interceptors if you have any depending on your use case

  .addInterceptor(new AkaOkHttpInterceptor())

  .build();

For more on MAP SDK usage, see our How-To: Using MAP SDK with Third-Party Image Libraries blog post.

Rahul Sharma is a solutions architect at Akamai Technologies.