Mobile App Server-to-Server Bidding Integration: Android Client Setup Guide

This guide describes how you can build an Android Client App. In the step-by-step guide below, we will be using the example of sending an Auction Request to your Auction Server on an interstitial ad. Make sure that you are already familiar with using Audience Network interstitial ads. Bidding also supports Native, Banner, Interstitial, In-stream Video and Rewarded Video formats. When you are using ad formats other than interstitial format, you can change your Server Settings.

Prerequisites

Android Client Side Setup Steps

Step 1: Making an auction request from Android client

Step 2: Loading the ad from bid response on Android client

Android Client Side Setup Steps

Step 1: Making an auction request from Android client

On the client side, we need to gather the required parameters for the auction request, and send it to the auction server using HTTP request. Here is an example implementation in Android for making the auction request specified by the format above:

private static JSONObject getRequest(
        Context context,
        String appId,
        String placementId,
        String idfa,
        int coppa,
        int dnt
) throws JSONException {
    JSONObject requestObject = new JSONObject();
    requestObject.put("app_id", appId);
    requestObject.put("placement_id", placementId);

    requestObject.put("bundle", BuildConfig.APPLICATION_ID);
    requestObject.put("bundle_version", BuildConfig.VERSION_NAME);

    requestObject.put("ifa", idfa);

    // coppa and do not track flags
    requestObject.put("coppa", coppa);
    requestObject.put("dnt", dnt);

    requestObject.put(
            "buyer_tokens",
            (new JSONObject())
                    .put("audience_network", getAudienceNetworkBidderToken(context))
    );

    requestObject.put("test", Settings.isTestMode() ? 1 : 0);

    return requestObject;
}

Here is the sample code snippet of a static function that sends the auction request to the server, and invoke the callback methods with the server response:

public static void makeRequest(
        final Context context,
        final int coppa,
        final String appId,
        final String placementId,
        final AuctionRequestCallback callback
) {
    getThreadPoolExecutor().submit(new Runnable() {
        @Override
        public void run() {
            try {
                // Retrieve idfa and limit ad tracking settings
                NonProductionIDFAUtils.AdIdInfo adInfo = NonProductionIDFAUtils.getAdIdInfo(context);

                final JSONObject request = getRequest(
                        context,
                        appId,
                        placementId,
                        adInfo.idfa,
                        coppa,
                        adInfo.dnt);

                if (Settings.getServerAddress() == null) {
                    throw new android.provider.Settings.SettingNotFoundException(
                            "Server address settings not found");
                }
                final URL url = new URL(Settings.getServerAddress());

                JsonObjectRequest jsObjRequest = new JsonObjectRequest(
                        Request.Method.POST,
                        Settings.getServerAddress(),
                        request,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                try {
                                    Log.d("response", response.toString());

                                    String placementId = response.getString("placement_id");
                                    String adFormat = response.getString("ad_format");

                                    String platformName = response.getString("platform_name");
                                    String platformPlacementId = response.getString(
                                            "platform_placement_id");
                                    String payload = response.getString("bid_payload");

                                    callback.onSuccess(
                                            placementId,
                                            adFormat,
                                            platformName,
                                            platformPlacementId,
                                            payload);

                                } catch (JSONException e) {
                                    callback.onError(e);
                                }
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                if (error.networkResponse == null) {
                                    callback.onError(new Exception("Server response empty!"));
                                } else {
                                    if(error.networkResponse.data != null) {
                                        callback.onError(new VolleyError(
                                                error.networkResponse.statusCode
                                                        + " " +
                                                        new String(error.networkResponse.data)));
                                    } else {
                                        callback.onError(new VolleyError("" +
                                                error.networkResponse.statusCode));
                                    }
                                }
                            }
                        }
                );

                getRequestQueue(context).add(jsObjRequest);

            } catch (Exception e) {
                // Other errors
                Log.e("error", e.toString());
                callback.onError(e);
            }
        }
    });
}

Step 2: Loading the ad from bid response on Android client

On the client app, when the HTTP request returns, the callback methods we specified above will be called. Using the response parameters, we can tell which platform won the auction, and load the ad. If the auction was successful and Audience Network won the auction, the response parameters will contain a payload string which we can load the ad from. We can call the loadAdFromBid method on the correct ad format class to load the ad. Here are the implemented auction request callback methods:

// AuctionRequestCallback
public void onSuccess(
        String placementId,
        String adFormat,
        String platformName,
        String platformPlacementId,
        String payload) {

    if (platformName.contentEquals(PLATFORM_AUDIENCE_NETWORK)) {
        if (adFormat.contentEquals(INTERSTITIAL)) {
            statusLabel.setText(R.string.loading_ad);

            interstitialAd = new InterstitialAd(MainActivity.this, platformPlacementId);
            interstitialAd.setAdListener(MainActivity.this);
            interstitialAd.loadAdFromBid(payload);
        }
    }
}

public void onError(Exception e) {
    Log.e(TAG, e.getMessage());
    statusLabel.setText(e.getMessage());
}

As a result, the sample app will load the ad if Audience Network won the bid, or display the error message received from the auction server.

What is Next

More Resources

Getting Started Guide

Technical guide to get started with Audience Network

Audience Network Mobile App Bidding Overview

Facebook Audience Network Mobile App Bidding architecture and other Concepts

Mobile App Server-to-Server Bidding Integration

Step-by-Step tutorial for mobile app server-to-server bidding integration

Audience Network Mobile SDK Downloads

Latest versions of Audience Network SDK downloads page