After updating Google Ads SDK onAdFailedToLoad is deprecated, How to resolve?

Viewed 2702

After updating Google Ads SDK to 19.3.0 gives a deprecated warning message for onAdFailedToLoad(). How can I resolve this?

My code:

public void BannerAdMob() {
        final AdView adView = findViewById(R.id.adsView);
        adView.loadAd(new AdRequest.Builder().build());
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                
            }

            @Override
            public void onAdFailedToLoad(int error) { // this method is deprecated 
                
            }
        });
    }
2 Answers

They added a new method you should override instead of that one:

public void onAdFailedToLoad (LoadAdError adError)

You can get a bunch of information from the adError object, calling getCode() on it will give you the code that the old method had.

please refer here for more info. the complete example as below:

    @Override
public void onAdFailedToLoad(LoadAdError error) {
  // Gets the domain from which the error came.
  String errorDomain = error.getDomain();
  // Gets the error code. See
  // https://developers.google.com/android/reference/com/google/android/gms/ads/AdRequest#constant-summary
  // for a list of possible codes.
  int errorCode = error.getCode();
  // Gets an error message.
  // For example "Account not approved yet". See
  // https://support.google.com/admob/answer/9905175 for explanations of
  // common errors.
  String errorMessage = error.getMessage();
  // Gets additional response information about the request. See
  // https://developers.google.com/admob/android/response-info for more
  // information.
  ResponseInfo responseInfo = error.getResponseInfo();
  // Gets the cause of the error, if available.
  AdError cause = error.getCause();
  // All of this information is available via the error's toString() method.
  Log.d("Ads", error.toString());
}
Related