Android でネイティブ広告を表示する

注:

ネイティブ インプレッションのカウント手法は、バナー クリエイティブで使用されるレンダリング カウント手法に従います。画面上の時間の長さに関係なく、ネイティブ広告がレンダリングされるとすぐにインプレッションが発生します。 これにより、精度と納品性が向上し、全体的な歩留まりが向上します。

ネイティブ広告を使用すると、アプリケーションのその他の部分の外観に合わせてカスタマイズされた広告を作成できます。 このページでは、ネイティブ広告 API の概要と使用例について説明します。

メディエーションでサポートされるネイティブ・ネットワーク:

  • Facebook
  • AdMob と DFP

ネイティブ広告を配信するには、ネイティブ広告リクエストを送信し、ネイティブ広告の応答を受け取ります。 Android 9 以降および API v. 28 以降では、ビューアビリティを正確に追跡するために、要求が既定で HTTPS である必要があります。 useHttps(true) で HTTPS を有効にすることができます。

次のコード例では、次のように行っています。

  • 要求オブジェクトを設定し、次のいずれかを提供します。

    • プレースメント ID (以下のコード例を参照)、または

    • インベントリ コードとメンバー ID の組み合わせ:

      public NativeAdRequest nativeAdRequest= new NativeAdRequest(context, "PLACEMENT_ID");
      //public NativeAdRequest nativeAdRequest= new NativeAdRequest(context, "INVENTORY_CODE", MEMBER_ID);
      
  • 必要に応じて、このNativeAdRequestrenderer_idを設定できます。 (renderer_idの詳細については、「ネイティブ レイアウト サービス」を参照してください。)vastxml、いいね、ダウンロード、セール価格、電話、住所、表示 URL をNativeAdResponseで返すには、renderer_idを指定する必要があります。

    nativeAdRequest.setRendererId(RENDERER_ID);
    
  • クリックなどのネイティブ広告イベント (NativeAdEventListener) を通知するリスナーを登録します。

  • リスナーを登録して、ネイティブ要求の状態 (成功または失敗) を通知します。 リスナーは NativeAdRequestListener インターフェイスを実装する必要があります。

  • 要求が成功した場合 (つまり、 NativeAdListener.onAdLoaded() が起動した場合)、ネイティブ広告アセットが NativeAdResponse オブジェクトに読み込まれ、アプリのネイティブな外観に一致するビューで使用できます。 次に、これらのビューの親ビューまたはコンテナー ビューを登録して、インプレッションとクリックのトラッキングを有効にします。

  • ワークフロー完了後、ネイティブ広告ビューの登録を解除します。 unregisterメソッドが呼び出されると、Xandr Mobile SDKのOMIDビューアビリティスクリプトは、ワークフロー中のOMIDセッションのバルクレポートを生成します。 そのため、パブリッシャーがこの API を実装して、視認性を正確に測定することが重要です。

注:

ネイティブ ビューおよびネイティブ応答オブジェクトへの参照を維持する。 ネイティブ ビューおよびネイティブ応答オブジェクトへの参照を維持する。

必要に応じて、ネイティブ広告ビューと NativeAdResponse オブジェクトへの参照を保持する責任があります。

public class MyActivity extends Activity {
 
    Context activityContext;
    NativeAdResponse nativeAdResponse;
    LinearLayout container;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        activityContext = this;
 
        // Create a NativeAdRequest object
        NativeAdRequest adRequest = new NativeAdRequest(activityContext, "123456"); // Placement ID
         
        // Optionally set the renderer_id
        //adRequest.setRendererId(123);
 
        // Create a listener for ad events
        NativeAdEventListener adEventListener = new
                NativeAdEventListener() {
                    @Override
                    public void onAdWasClicked() {
                        // Do something when the view is clicked
                    }
 
                    @Override
                    public void onAdWillLeaveApplication() {
                        // Do something when the ad is taking user away from current app
                    }
 
                    @Override
                    public void onAdWasClicked(String clickUrl, String fallbackURL) {
                        // Handle Click URL
                    }
                };
 
        // Whether to pre-load the native ad's icon and main image
        adRequest.shouldLoadIcon(true);
        adRequest.shouldLoadImage(true);
 
        adRequest.setListener(new NativeAdRequestListener() {
            @Override
            public void onAdLoaded(NativeAdResponse response) {
                nativeAdResponse = response;
                // Cover image
                ImageView imageView = new ImageView(activityContext);
                imageView.setImageBitmap(response.getImage());
 
                // Icon image
                ImageView iconView = new ImageView(activityContext);
                iconView.setImageBitmap(response.getIcon());
 
                // Title
                TextView title = new TextView(activityContext);
                title.setText(response.getTitle());
 
                // Main text
                TextView description = new TextView(activityContext);
                description.setText(response.getDescription());
 
                // Text that indicates a call to action -- for example, to install an app
                TextView callToAction = new TextView(activityContext);
                callToAction.setText(response.getCallToAction());
 
                // Create a container (a parent view that holds all the
                // views for native ads)
                LinearLayout container = new LinearLayout(activityContext);
                container.addView(iconView);
                container.addView(title);
 
                // Add the native ad container to the view hierarchy
                LinearLayout ad_frame = findViewById(R.id.native_ad_frame);
                ad_frame.addView(container);
            }
 
            @Override
            public void onAdFailed(ResultCode errorcode) {
 
            }
        });
 
        // Call loadAd() to request a response once
        adRequest.loadAd();
 
        // Register native views for click and impression tracking.  The
        // adEventListener is the listener created above; it can be null if
        // you don't want to receive notifications about click events.
        // Impressions and clicks won't be counted if the view is not registered.
        NativeAdSDK.registerTracking(nativeAdResponse, container, adEventListener);
 
        // It's your responsibility to keep a reference to the view
        // and NativeAdResponse object if necessary.
        // Once done with the native ad view, call the following method to
        // unregister that view.
        NativeAdSDK.unRegisterTracking(container);
    }
}
    

ネイティブでサポートされているフィールド

Mobile SDK のバージョン 5.0 以降、ネイティブ アセットのサポートは、Xandr の UI でネイティブ クリエイティブが設定される方法に合わせて調整されています。

まだレガシ ネイティブを使用している場合は、クリエイティブ用に "新しい" ネイティブに移行する必要があります。

SDK でサポートされているネイティブ アセットの包括的な一覧を次に示します。

Asset 5.0 以前でサポートされていますか? ポスト 5.0 がサポートされていますか? v5.0+ API-Usage 例
画像、幅、高さ はい、はい、はい はい、はい、はい nativeAdResponse.getImage();
nativeAdResponse.getImageSize();
nativeAdResponse.getImageUrl();
アイコン + 幅 + 高さ はい、いいえ、いいえ はい、はい、はい nativeAdResponse.getIcon();
nativeAdResponse.getIconSize();
nativeAdResponse.getIconUrl();
タイトル はい はい nativeAdResponse.getTitle();
後援 はい はい nativeAdResponse.getSponsoredBy();
Body text はい はい nativeAdResponse.getDescription();
Desc2 はい はい nativeAdResponse.getAdditionalDescription();
行動を促すフレーズ はい はい nativeAdResponse.getCallToAction();
レーティング、スケール はい、はい はい、いいえ nativeAdResponse.getAdStarRating();
いいね! 不要 はい (json のみ) if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT));

String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); }
ダウンロード 不要 はい (json のみ) if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT));

String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); }
Price 不要 はい (json のみ) if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT));

String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); }
販売価格 不要 はい (json のみ) if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT));

String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); }
Phone 不要 はい (json のみ) if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT));

String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); }
アドレス 不要 はい (json のみ) if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT));

String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); }
表示 URL 不要 はい (json のみ) if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT));

String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); }
クリック URL 不要 はい (json のみ) if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT));

String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); }
[フォールバック URL] をクリックします 不要 はい (json のみ) if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT));

String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); }
プライバシー URL 不要 はい nativeAdResponse.getPrivacyLink();
ビデオ 不要 はい nativeAdResponse.getVastXml();
Custom はい 不要
Context はい 不要
全文 はい 不要

OpenRTB ネイティブ

OpenRTB ネイティブとは、NativeAdRequest および NativeAdResponse クラスで OpenRTB ネイティブ アセット仕様を使用することを指し、より柔軟で標準化された広告要求と応答を可能にします。 OpenRTB Native 1.2 仕様標準の詳細については、 OpenRTB Native Ads Specification.1.2 を参照してください。

注:

OpenRTB ネイティブは、すべてのメンバーが利用できるわけではありません。 ご不明な点がある場合は、担当のアカウント マネージャーにお問い合わせいただくか、サポートにお問い合わせください。

NativeAdRequest の ORTB

OpenRTB ネイティブを使用するには、アプリで setOpenRTBAssets(JSONObject openRTBAssets) メソッドを使用して、NativeAdRequest で OpenRTB ネイティブ アセットを指定する必要があります。 このフィールドの内容は、OpenRTB ネイティブ 1.2 要求マークアップに従って、OpenRTB ネイティブ要求である必要があります。

要求用のコード サンプル Java

NativeAdRequest nativeAdRequest = new NativeAdRequest(context, "PLACEMENT_ID");
String ortbJSONString = "{\"ver\":\"1.2\",\"assets\":[{\"id\":1,\"required\":1,\"title\":{\"len\":300}},{\"id\":2,\"required\":1,\"data\":{\"type\":2}},{\"id\":3,\"required\":1,\"data\":{\"type\":1}},{\"id\":4,\"required\":1,\"image\":{\"type\":3}},{\"id\":5,\"required\":0,\"data\":{\"type\":555,\"len\":45}}]}";
nativeAdRequest.setOpenRTBAssets(new JSONObject(ortbJSONString));

要求用 Kotlin のコード サンプル


var nativeAdRequest = NativeAdRequest(context, "PLACEMENT_ID")
val ortbJSONString = "{\"ver\":\"1.2\",\"assets\":[{\"id\":1,\"required\":1,\"title\":{\"len\":300}},{\"id\":2,\"required\":1,\"data\":{\"type\":2}},{\"id\":3,\"required\":1,\"data\":{\"type\":1}},{\"id\":4,\"required\":1,\"image\":{\"type\":3}},{\"id\":5,\"required\":0,\"data\":{\"type\":555,\"len\":45}}]}"
nativeAdRequest.openRTBAssets = JSONObject(ortbJSONString);

注:

このアプリでは、eventtrackers 配列を指定する必要はありません。 SDK は、サポートされている値を使用して eventtrackers 配列に自動的に設定します。 アプリが値を指定しても、SDK によってオーバーライドされます。

NativeAdResponse の ORTB

応答の ORTB は、NativeAdResponse クラスのパブリック JSONObject getOpenRTBNative() メソッドを使用してアプリに公開される生の ORTB ネイティブ JSON に対応します。 リクエストと同様に、応答 JSONObject は OpenRTB ネイティブ 1.2 応答マークアップに従います。詳細については、「 OpenRTB ネイティブ広告仕様 .1.2 」を参照してください。

getOpenRTBNative() を介して生の ORTB ネイティブ応答 JSONObject を公開することに加えて、SDK は、getTitle()、getDescription()、getImageUrl()、getImageImage()、getImageSize()、getIconUrl()、getIcon()、getIconSize()、getCallToAction()、getAdStarRating()、getSponsoredBy()、getVastXml()、getPrivacyLink() などのメソッドを使用して、標準の ORTB ネイティブ応答アセット (img、タイトル、ビデオ、データ) を解析して利用できるようにします。 また、SDK はインプレッション、視認性、クリック トラッキングを自動的に処理します。

注:

アプリは、ネイティブ広告がレンダリングされるビューを SDK に登録する必要があります。 SDK は、インプレッション、クリック、視認性のトラッキングを処理します。 アプリは、ORTB ネイティブ応答 JSONObject で eventtrackers、imptrackers、jstracker、または clicktrackers を受け取りません。

応答用のコード サンプル Java

@Override
public void onAdLoaded(NativeAdResponse nativeAdResponse) {

    // Network type Network.APPNEXUS_ORTB indicates the response is ORTB
    if (nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS_ORTB) {
        JSONObject ortbNativeResponseJSON = nativeAdResponse.getOpenRTBNative();

        // App has the option to either
        // 1. Use the parsed title, description, etc., which the SDK readily makes available
        // and dip into the ORTB Native response JSON for non-parsed/custom values
        // OR
        // 2. Handle all the ORTB Native response JSON response parsing in the app

        // Accessing title, description etc using the getters exposed in
        // NativeAdResponse class
        String title = nativeAdResponse.getTitle();
        String description = nativeAdResponse.getDescription();
        String imageUrl = nativeAdResponse.getImageUrl();
        NativeAdResponse.ImageSize imageSize = nativeAdResponse.getImageSize();
        
        
        // Parsing ORTB Native resposnse JSON
        // Extract "assets" array
        JSONArray assetsArray = ortbNativeResponseJSON.getJSONArray("assets");
        for (int i = 0; i < assetsArray.length(); i++) {
            JSONObject asset = assetsArray.getJSONObject(i);
            // This id will match the id provided in the request.
            // This id is essential for matching the various data assets in the request
            // with the response.            
            int id = asset.getInt("id");
            System.out.println("Asset ID: " + id); 

            // Check for "title"
            if (asset.has("title")) {
                String assetTitle = asset.getJSONObject("title").getString("text");
                System.out.println("Title: " + assetTitle);
            }

            // Check for "data"
            if (asset.has("data")) {
                String data = asset.getJSONObject("data").getString("value");
                System.out.println("Data: " + data);
            }

            // Check for "image"
            if (asset.has("image")) {
                JSONObject image = asset.getJSONObject("image");
                String url = image.getString("url");
                int width = image.getInt("w");
                int height = image.getInt("h");
                System.out.println("Image URL:"+url+",Width:"+width+",Height:"+height);
            }
        }
    }
}

応答用のコード サンプル Kotlin


override fun onAdLoaded(nativeAdResponse: NativeAdResponse) {

    // Network type Network.APPNEXUS_ORTB indicate the response is ORTB
    if(nativeAdResponse.networkIdentifier == NativeAdResponse.Network.APPNEXUS_ORTB) {
        var ortbNativeResponseJSON = nativeAdResponse.openRTBNative

        // App has the option to either
        // 1. Use the parsed title,description etc which the SDK readily makes available
        // and dip into the ORTB Native response JSON for non-parsed/custom values
        // OR
        // 2. Handle all the ORTB Native response JSON response parsing in the app

        // Accessing title, description etc using the getters exposed in
        // NativeAdResponse class
        nativeAdResponse.title
        nativeAdResponse.description
        nativeAdResponse.imageUrl
        nativeAdResponse.imageSize


        // Parsing ORTB Native resposnse JSON
        // Extract "assets" array
        val assetsArray = ortbNativeResponseJSON.getJSONArray("assets")
        for (i in 0 until assetsArray.length()) {
            val asset = assetsArray.getJSONObject(i)
            // This id will match the id provide in request,
            // This id is essential for matching the various data assets in req with response            
            val id = asset.getInt("id")
            println("Asset ID: $id") 

            // Check for "title"
            if (asset.has("title")) {
                val title = asset.getJSONObject("title").getString("text")
                println("Title: $title")
            }

            // Check for "data"
            if (asset.has("data")) {
                val data = asset.getJSONObject("data").getString("value")
                println("Data: $data")
            }

            // Check for "image"
            if (asset.has("image")) {
                val image = asset.getJSONObject("image")
                val url = image.getString("url")
                val width = image.getInt("w")
                val height = image.getInt("h")
                println("Image URL: $url, Width: $width, Height: $height")
            }
        }
    }

例: ORTB ネイティブ応答 NativeAdResponse の JSONObject 構造体


 {
       "ver": "1.2",
       "assets": [
      {
          "id": 1,
          "title": {
              "text": "Sample Title here."
          }
       },{    
          "id": 2,
          "data": {
              "value": "Sample description text here."
           }
      }, {
          "id": 3,
          "data": {
              "value": "Sample Sponsored by text here."
          }
      }, {
          "id": 4,
          "image": {
              "url": "https://sample.img.url/here.jpg",
              "w": 123,
              "h": 234
          }
      }, {
          "id": 5,
          "data": {
              "value": "Sample disclaimer value here."
          }
      }
   ],
  // remaining ortb native 1.2 response fields would be here;
  // ie for link,privacy etc if available
}