Reputation: 321
I followed the Mopub instructions to implement their native ads in my Android App.
I checked the code with other sources and apparently, everything seems alright, but actually I cannot get Mopub to load any ad. My code is:
SdkConfiguration sdkConfiguration = new SdkConfiguration
.Builder("11a17b188668469fb0412708c3d16813") // trial #
.build();
MoPub.initializeSdk(this, sdkConfiguration, initSdkListener());
MoPubNative moPubNative = new MoPubNative(this, "11a17b188668469fb0412708c3d16813", this); // 11a17b188668469fb0412708c3d16813
ViewBinder viewBinder = new ViewBinder.Builder(R.layout.mopub_native_ad_list_item)
.mainImageId(R.id.native_main_image)
.iconImageId(R.id.native_icon_image)
.titleId(R.id.native_title)
.textId(R.id.native_text)
.privacyInformationIconImageId(R.id.native_privacy_information_icon_image)
.build();
MoPubStaticNativeAdRenderer moPubStaticNativeAdRenderer = new MoPubStaticNativeAdRenderer(viewBinder);
moPubNative.registerAdRenderer(moPubStaticNativeAdRenderer);
EnumSet<RequestParameters.NativeAdAsset> desiredAssets = EnumSet.of( // Optional
RequestParameters.NativeAdAsset.TITLE,
RequestParameters.NativeAdAsset.TEXT,
RequestParameters.NativeAdAsset.CALL_TO_ACTION_TEXT,
RequestParameters.NativeAdAsset.MAIN_IMAGE,
RequestParameters.NativeAdAsset.ICON_IMAGE,
RequestParameters.NativeAdAsset.STAR_RATING
);
// RequestParameters mRequestParameters = new RequestParameters.Builder().desiredAssets(desiredAssets).build();
moPubNative.makeRequest();
And the code of callbacks:
private SdkInitializationListener initSdkListener() {
return new SdkInitializationListener() {
@Override
public void onInitializationFinished() {
/* MoPub SDK initialized.
Check if you should show the consent dialog here, and make your ad requests. */
}
};
}
@Override
public void onNativeLoad(final com.mopub.nativeads.NativeAd nativeAd) {
FrameLayout mopubGloFrameLayout = (FrameLayout) findViewById(R.id.flurrymainframe); // Parent where to attach the Native Ad
View adView = nativeAd.createAdView(getBaseContext(), mopubGloFrameLayout);
nativeAd.prepare(adView);
nativeAd.renderAdView(adView);
mopubGloFrameLayout.addView(adView);
}
@Override
public void onNativeFail(final NativeErrorCode errorCode) {
Log.w(this.getClass().getSimpleName(), errorCode.toString());
}
@Override
public void onImpression(final View view) {
// Ad impression has been recorded on MoPub and the corresponding ad network
}
@Override
public void onClick(final View view) {
// Ad click has been recorded on MoPub and the corresponding ad network
}
And a snapshot of the MoPub Native variables after moPubNative.makeRequest() :
My relevant logcat:
01-21 03:36:13.534 3936-3979/com.languagematerial.lmmovies E/FlurryAgent: GOOGLE PLAY SERVICES ERROR: java.util.concurrent.TimeoutException: Timed out waiting for the service connection
01-21 03:36:13.535 3936-3979/com.languagematerial.lmmovies E/FlurryAgent: There is a problem with the Google Play Services library, which is required for Android Advertising ID support. The Google Play Services library should be integrated in any app shipping in the Play Store that uses analytics or advertising.
01-21 03:36:23.539 3936-3979/com.languagematerial.lmmovies E/FlurryAgent: GOOGLE PLAY SERVICES ERROR: java.util.concurrent.TimeoutException: Timed out waiting for the service connection
01-21 03:36:23.539 3936-3979/com.languagematerial.lmmovies E/FlurryAgent: There is a problem with the Google Play Services library, which is required for Android Advertising ID support. The Google Play Services library should be integrated in any app shipping in the Play Store that uses analytics or advertising.
01-21 03:36:26.405 3936-3936/com.languagematerial.lmmovies D/MoPub: Initializing MoPub with ad unit: 11a17b188668469fb0412708c3d16813
01-21 03:36:26.425 3936-3936/com.languagematerial.lmmovies D/MoPub: MoPubNative Loading ad from: https://ads.mopub.com/m/ad?id=11a17b188668469fb0412708c3d16813&nv=5.4.1&dn=unknown%2CAndroid%20SDK%20built%20for%20x86%
2Csdk_google_phone_x86&bundle=com.languagematerial.lmmovies&z=-0500&o=l&w=1024&h=600&sc=1.0&mcc=310&mnc=260&cn=Android&ct=6&av=1.0&udid=mp_tmpl_advertising_id&dnt=mp_tmpl_do_not_track&gdpr_applies=0&force_gdpr_applies=0¤t_consent_status=unknown
01-21 03:36:26.427 3936-3936/com.languagematerial.lmmovies E/MoPub: Make sure to call MoPub#initializeSdk before loading an ad.
01-21 03:36:26.437 3936-4009/com.languagematerial.lmmovies E/Volley: [236] NetworkDispatcher.processRequest: Unhandled exception java.lang.RuntimeException: Bad URL
java.lang.RuntimeException: Bad URL
at com.mopub.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:171)
at com.mopub.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120)
at com.mopub.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)
Caused by: java.net.MalformedURLException: Protocol not found:
at java.net.URL.<init>(URL.java:176)
at java.net.URL.<init>(URL.java:125)
at com.mopub.volley.toolbox.HurlStack.executeRequest(HurlStack.java:92)
at com.mopub.network.RequestQueueHttpStack.executeRequest(RequestQueueHttpStack.java:57)
at com.mopub.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:131)
at com.mopub.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120)
at com.mopub.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)
01-21 03:36:26.449 1263-1686/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
01-21 03:36:26.449 3936-4015/com.languagematerial.lmmovies D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-21 03:36:26.453 3936-3967/com.languagematerial.lmmovies W/chromium: [WARNING:data_reduction_proxy_config.cc(423)] SPDY proxy OFF at startup
01-21 03:36:26.456 3936-3936/com.languagematerial.lmmovies W/TextToSpeech: speak failed: not bound to TTS engine
01-21 03:36:26.456 3936-3936/com.languagematerial.lmmovies I/TextToSpeech: Connected to ComponentInfo{com.svox.pico/com.svox.pico.PicoService}
01-21 03:36:26.461 3936-4016/com.languagematerial.lmmovies I/TextToSpeech: Set up connection to ComponentInfo{com.svox.pico/com.svox.pico.PicoService}
01-21 03:36:26.488 3936-3936/com.languagematerial.lmmovies W/TextToSpeech: speak failed: TTS engine connection not fully set up
01-21 03:36:26.488 3936-3936/com.languagematerial.lmmovies D/MoPub: Native ad request failed.
com.mopub.network.MoPubNetworkError: java.lang.RuntimeException: Bad URL
at com.mopub.network.AdLoader.deliverError(AdLoader.java:275)
at com.mopub.network.AdLoader.access$200(AdLoader.java:30)
at com.mopub.network.AdLoader$1.onErrorResponse(AdLoader.java:85)
at com.mopub.volley.Request.deliverError(Request.java:630)
at com.mopub.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:108)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Bad URL
at com.mopub.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:171)
at com.mopub.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120)
at com.mopub.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)
Caused by: java.net.MalformedURLException: Protocol not found:
at java.net.URL.<init>(URL.java:176)
at java.net.URL.<init>(URL.java:125)
at com.mopub.volley.toolbox.HurlStack.executeRequest(HurlStack.java:92)
at com.mopub.network.RequestQueueHttpStack.executeRequest(RequestQueueHttpStack.java:57)
at com.mopub.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:131)
at com.mopub.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120)
at com.mopub.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)
01-21 03:36:26.489 3936-3936/com.languagematerial.lmmovies W/LMMoviesMainActivity: Unspecified error occurred.
01-21 03:36:26.490 3936-4006/com.languagematerial.lmmovies D/MoPub: MoPubIdentifier initialized.
01-21 03:36:26.491 3936-4010/com.languagematerial.lmmovies D/MoPub: Posting AsyncTask to main thread for execution.
Notice that I get this warning:
Make sure to call MoPub#initializeSdk before loading an ad.
According to variables in the snapshot, I managed to load something, so why it says it didn't?
I just don't know what else to do. There are similar cases of MoPub Native failure in stackoverflow, but none in reality proposes any solution, not even alternatives to get one closer. And MoPub pseudosupport sucks. Have you by chance implemented MoPub Native Ads successfully? If so, can you see what could be wrong with my code? Thanks
Upvotes: 1
Views: 1333
Reputation: 81
Put this code in ocCreate method to initialize sdk and you can also check if it is initialized or not by using onInitializationFinished method by SdkInitializationListener interface
SdkConfiguration sdkConfiguration = new SdkConfiguration.Builder("MOPUB_ID")
.withLogLevel(MoPubLog.LogLevel.DEBUG)
.withLegitimateInterestAllowed(false)
.build();
MoPub.initializeSdk(this, sdkConfiguration, new SdkInitializationListener() {
@Override
public void onInitializationFinished() {
Log.d("Mopub", "SDK initialized");
}
});
Upvotes: 1
Reputation: 21
I faced the same problem, and resolved it with newer version mopub-android-sdk library(5.5.0).
Can you try this instead of mopubNative.makeRequest()
? (this is kotlin code.)
MoPub.initializeSdk(this, sdkConfiguration, object : SdkInitializationListener {
override fun onInitializationFinished() {
mopubNative.makeRequest()
}
})
Upvotes: 1