Using MAP/mPulse SDK with Third-Party Network Libraries

MAP/mPulse SDK supports the most commonly used, third-party network libraries including:

  • HttpUrlConnection
  • OkHttpClient
  • Retrofit

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

 

  1. HttpUrlConnection

    MAP/mPulse 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/mPulse SDK will automatically intercept the traffic and provide desired feature functionality.

  2. OkHttpClient

    OkHttpClient is commonly used as a networking library in Android to get data from a server. MAP/mPulse SDK supports OkHttpClient and provide desired feature functionality. The integration of MAP/mPulse SDK and OkHttpClient is simple and straightforward. MAP/mPulse 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/mPulse SDK.

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

    Download - AkaOkHttpInterceptor

    Sample Code:

    OkHttpClient client = new OkHttpClient.Builder()
           .addInterceptor(new AkaOkHttpInterceptor())
           .build();
    Request request = new Request.Builder()
           .url(uri)
           .build();

    Response response = client.newCall(request).execute();

  3. Retrofit

    Retrofit is another networking library used in Android to fetch data from a server. MAP/mPulse SDK supports Retrofit and provides desired feature functionality.

    Retrofit Version 2 by default uses OkHttpClient as networking client to get data from a server. MAP/mPulse 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.

    Download - AkaRetrofit2Client

    Sample Code:

    Retrofit retrofit = new Retrofit.Builder()
             .baseUrl(uri)
             .addConverterFactory(GsonConverterFactory.create())
             .client(AkaRetrofit2Client.getClient())
             .build();

    Retrofit2Api client = retrofit.create(Retrofit2Api.class);
    Call call = client.get(path);

     

Using MAP/mPulse SDK with Third-Party Image Libraries

 

  1. Picasso

    Picasso is the most commonly used image loading library in Android. Let’s look at how to make MAP/mPulse SDK work with Picasso.

    For Picasso - version 2.71828 and above

    Download AkaPicassoDownloader

    Sample Code:

    Picasso picasso = new Picasso.Builder(getContext())
              .downloader(new OkHttp3Downloader(AkaPicassoDownloader.getClient()))
              .build();
    Picasso.setSingletonInstance(picasso);

    For Picasso - version 2.5.2 and below

    Download wrapper for Picasso v2.5.2 and below

    Sample Code:

    Picasso picasso = new Picasso.Builder(getContext())
             .downloader(new AkaPicassoDownloader_upto_v2_5_2(getContext()))
             .build();
    Picasso.setSingletonInstance(picasso);

  2. Glide

    Default Glide (without any additional networking client)

    By default, Glide uses HttpUrlFetcher.java, which then internally leverages 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/mPulse SDK will automatically intercept the traffic and provide desired feature functionality.

    Glide with OkHttp3

    If Glide is used with OkHttp3, then Glide internally uses OkHttp3Stack and OkHttpClient as the networking client to fetch images from the server. In this scenario, OkHttpClient is used as the networking client by Glide library, and therefore MAP/mPulse SDK will not intercept image traffic. Hence, we will need to use an interceptor to capture image traffic through MAP/mPulse SDK.

    To do this, use AkaOkHttpInterceptor with OkHttpClient while registering the component for Glide in AppGlideModule

    Download AkaGlideClient

    Sample Code:


    @GlideModule
    @Excludes(OkHttpLibraryGlideModule.class)
    public class AppGlide extends AppGlideModule {
     
    @Override
     public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
      super.registerComponents(context, glide, registry);
      ...
      registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(AkaGlideClient.getClient()));
     }
    }

  3. Universal Image Loader

    By default, Android Universal Image Loader internally uses 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.

    Note: If you are using any other 3rd party library other than the mentioned above, please let us know we could help you to get the wrapper for the same.

 

Contact Us

Questions?

If you have any questions, feel free to drop us a line!

Please contact us through our community page or via sdk-sme@akamai.com if you need assistance in debugging integration/upgrade issues.