Search code examples
androidandroid-studioadmob

Admob Interstitial ad showing Error building request URL


I have implemented admob interstitial ad recently and getting exception/error as Error building request URL. from method onAdFailedToLoad(), and also my ad is not shown .

My code:

InterstitialAd interstitialAd;
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
        
                InterstitialAd interstitialAd = new InterstitialAd(MainActivity.this);
                interstitialAd.setAdUnitId(KEY);
                MobileAds.initialize(MainActivity.this, new OnInitializationCompleteListener() {
                    @Override
                    public void onInitializationComplete(InitializationStatus initializationStatus) {
                        Log.e("app","MobileAds onInitializationComplete");
                    }
                });
                interstitialAd.loadAd(new AdRequest.Builder().build());
                interstitialAd.setAdListener(new AdListener(){
                    @Override
                    public void onAdLoaded() {
                        Log.e("app","ad loaded");
                    }
        
                    @Override
                    public void onAdFailedToLoad(LoadAdError loadAdError) {
                        Log.e("app","ad failedToLoad: "+loadAdError.getMessage()); //error/exception here
                    }
                });
}
                  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        if(interstitialAd.isLoaded()) {
                    interstitialAd.show();
                    Log.e("app","ad loaded");
                }
                else Log.e("app","ad not loaded yet");              
    }
}

I have registered my id properly in the Manifest.xml

<application>
<meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="@string/KEY"/>
    </application>

Followed same guideline shown in here


Solution

  • Make sure you use the correct test ad ID from AdMob in the call InterstitialAd. This is not the same as your App ID, it has an attached slash and a number behind it.

    For testing, use ca-app-pub-3940256099942544/1033173712 which is the test ID given by the docs at the time of writing.