序列化和反序列化PublisherInterstitialAd对象

时间:2016-06-14 06:58:11

标签: serialization sharedpreferences deserialization google-dfp android gson

是否可以对Google DFP广告的PublisherInterstitialAd对象进行序列化和反序列化,以便在SharedPreferences中保存和检索对象?

当我尝试使用Gson库时,我得到了StackOverflowError。请建议如何做到最好,以及在我目前的方法中我会出错的地方。

谢谢。

这些是用于在SharedPreferences中保存和检索Ad对象的方法,其中publisherInterstitialAd是相关对象。

public void saveInterstitialAd(PublisherInterstitialAd publisherInterstitialAd) {
    Gson gson = new Gson();
    String json = gson.toJson(publisherInterstitialAd);
    mEditor.putString("InterstitialAd", json);
    mEditor.commit();
}

public PublisherInterstitialAd getInterstitialAd(){
    Gson gson = new Gson();
    String json = mSharedPrefs.getString("InterstitialAd", "");
    if(json.equals(""))
        return null;
    return gson.fromJson(json, PublisherInterstitialAd.class);
}

这是我正在获得的堆栈跟踪:

UncaughtException                                                                      java.lang.StackOverflowError的                                                                          在java.lang.Class.isArray(Class.java:1118)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:96)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:551)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:544)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:551)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:544)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:551)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:544)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:551)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:544)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:551)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:544)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:551)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:544)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:551)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:544)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:551)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:544)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:551)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:544)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:551)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:544)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:551)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:544)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:551)                                                                          在com.google.gson.internal。$ Gson $ Types.canonicalize($ Gson $ Types.java:109)                                                                          在com.google.gson.internal。$ Gson $ Types $ WildcardTypeImpl。($ Gson $ Types.java:544)                                                                         在com.google.gson.internal。$ Gson $ Types.canonical

1 个答案:

答案 0 :(得分:0)

我认为谷歌会争辩说你不应该长期存储广告。他们必须意识到他们的广告对象不可序列化或可分配,因此这让我相信它是故意的。

有人说,如果你真的必须,你可以随时将对象存储在你的Application对象上(我真的建议不要这样做,因为这是一个充满陷阱的严重反模式)。我建议您只是在最终可能会显示的任何活动中询问插页式广告(例如,如果您的目标是在活动完成后显示插页式广告,则可能会在活动开始时加载插页式广告创建,然后在用户提交了您想要的任何操作后,在完成活动之前显示它。