无法使MoPub将原生广告投放到我的Android应用

时间:2019-01-21 09:13:11

标签: android advertisement mopub

我按照Mopub的说明在我的Android应用中实施了其原生广告。

我与其他来源检查了代码,显然,一切似乎都不错,但是实际上我无法让Mopub加载任何广告。我的代码是:

 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();

以及回调代码:

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
}

以及moPubNative.makeRequest()之后的MoPub本机变量的快照:

MoPub Native variables after moPubNative.makeRequest()

我的相关日志猫:

  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&current_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.

请注意,我收到此警告:

确保在加载广告之前调用MoPub#initializeSdk。

根据快照中的变量,我设法加载了一些东西,为什么它说没有呢?

我只是不知道该怎么办。在堆栈溢出中也有类似的MoPub Native失败案例,但实际上没有人提出任何解决方案,甚至没有提出任何解决方案。 MoPub伪支持很烂。您是否偶然成功实施了MoPub原生广告?如果是这样,您能看到我的代码有什么问题吗?谢谢

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,并使用较新版本的mopub-android-sdk库(5.5.0)解决了该问题。

您可以代替mopubNative.makeRequest()来尝试吗? (这是kotlin代码。)

 MoPub.initializeSdk(this, sdkConfiguration, object : SdkInitializationListener {
            override fun onInitializationFinished() {
                mopubNative.makeRequest()
            }
        })

答案 1 :(得分:0)

将此代码放在ocCreate方法中以初始化sdk,并且您还可以通过SdkInitializationListener接口的onInitializationFinished方法来检查它是否已初始化

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");
        }
    });
相关问题