Ce document a été mis à jour.
La traduction en Français (France) n’est pas encore terminée.
Anglais mis à jour : 5 août 2021
Français (France) mis à jour : 26 nov. 2018

Server-to-Server: Android Client Setup Guide

In-house mediation is not publicly available

In-house bidding with Audience Network is currently in Closed Beta and is not publicly available. We'll provide further updates if this changes.

As an alternative, you can access Audience Network Bidding through one of the mediation platforms we partner with.

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 loadAd(...) method on the correct ad format class and include the payload string as bid in the `LoadAdConfig 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.loadAd(
                    interstitialAd.buildLoadAdConfig()
                            .withAdListener(MainActivity.this)
                            .withBid(payload)
                            .build());
        }
    }
}

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.