我使用以下代码从dfp请求广告,但我想在一个请求中请求多个广告并将它们存储在arraylist(或类似的数据结构)中,并在需要时在多个位置使用它们。
我尝试将请求放在for循环中,但似乎没有工作。
以下是我用于从dfp
获取广告的代码private void refreshAd() {
templateAdLoadedListener = new NativeCustomTemplateAd.OnCustomTemplateAdLoadedListener() {
@Override
public void onCustomTemplateAdLoaded(NativeCustomTemplateAd nativeCustomTemplateAd) {
Log.d(TAG, "onCustomTemplateAdLoaded: ad fetched");
customTemplateAds.add(nativeCustomTemplateAd);
}
};
templateAdClickListener = new NativeCustomTemplateAd.OnCustomClickListener() {
@Override
public void onCustomClick(NativeCustomTemplateAd nativeCustomTemplateAd, String s) {
Intent intent = new Intent(MainActivity.this, AboutWebActivity.class);
intent.putExtra("TYPE", "AD");
intent.putExtra("TITLE", "AD TITLE");
Log.d(TAG, "onCustomClick: url = " + String.valueOf(nativeCustomTemplateAd.getText("Click-through URL")));
intent.putExtra("URL", String.valueOf(nativeCustomTemplateAd.getText("Thirdpartyclicktracker")));
startActivity(intent);
}
};
adLoader = new AdLoader.Builder(MainActivity.this,"ad_unit_id")
.forCustomTemplateAd("ad_template_id", templateAdLoadedListener, templateAdClickListener)
.withAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) {
Log.d(TAG, "onCustomTemplateAdFailedToLoad: error code = " + errorCode);
}
}).build();
adLoader.loadAd(new PublisherAdRequest.Builder().build());
}
我尝试将 refreshAd()函数放在for循环中,但它无效。在广告单个请求中获取多个广告的最佳方式是什么?