Retrofit 没有在我的响应中序列化第二个对象 class
Retrofit doesn't serialize second object in my response class
我的问题是当 运行 我的应用程序发布 APK(proguard 已启用)时,GSON 没有序列化我的第二个对象。第一个对象已成功序列化。但是,应用程序在调试模式下运行一切正常。
我的应用使用 Retrofit2.6 和 Proguard。 Gradle 版本为 gradle:3.5.1
我的 json 来自网络服务器的数据
{
"error": false,
"contents": [
{
"id": 1,
"channel": {
"id": 7,
"language_id": 1
},
"publisher": {
"id": 1,
"name": "Name of Publisher"
},
"title": "Some title",
"description": "This is description",
"subscribed": false
}
]
}
这是 Retrofit
的响应 class
public class GetContentsResponse{
@SerializedName("error")
@Expose
private boolean mError;
@SerializedName("contents")
@Expose
private List<Content> mContents;
}
public class Content {
@SerializedName("id")
@Expose
private int mId;
@SerializedName("channel")
@Expose
private Channel mChannel;
@SerializedName("publisher")
@Expose
private Publisher mPublisher;
@SerializedName("title")
@Expose
private String mTitle;
@SerializedName("description")
@Expose
private String mDescription;
@SerializedName("subscribed")
@Expose
private boolean mSubscribed;
public int getId() {
return mId;
}
public Channel getChannel() {
return mChannel;
}
public String getTitle() {
return mTitle;
}
public String getDescription() {
return mDescription;
}
public boolean isSubscribed() {
return mSubscribed;
}
public Publisher getPublisher() {
return mPublisher;
}
}
这是我的混淆规则
-dontwarn javax.annotation.**
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase
-dontwarn org.codehaus.mojo.animal_sniffer.*
-dontwarn okhttp3.internal.platform.ConscryptPlatform
-keepattributes *Annotation*
Publisher 对象正在调试模式下解析(proguard 已禁用)。当 Release APK 运行时,Publisher 对象为 null(proguard 已启用)。
我真的很研究这个问题。但我不明白问题是什么?有没有人可以帮忙?
编辑:我暂时将所有模型 classes 保留在 Proguard 规则中,但这里有一个神秘的错误。我找不到它。为什么 Channel 对象序列化而 Publisher 对象没有序列化?
在你的proguard中添加这个
-keep public class com.package.name.models.** { *; }
注意:- com.package.name.models - 这是您的 POJO class 所在的包名称。
就是这样。
我的问题是当 运行 我的应用程序发布 APK(proguard 已启用)时,GSON 没有序列化我的第二个对象。第一个对象已成功序列化。但是,应用程序在调试模式下运行一切正常。
我的应用使用 Retrofit2.6 和 Proguard。 Gradle 版本为 gradle:3.5.1
我的 json 来自网络服务器的数据
{
"error": false,
"contents": [
{
"id": 1,
"channel": {
"id": 7,
"language_id": 1
},
"publisher": {
"id": 1,
"name": "Name of Publisher"
},
"title": "Some title",
"description": "This is description",
"subscribed": false
}
]
}
这是 Retrofit
的响应 classpublic class GetContentsResponse{
@SerializedName("error")
@Expose
private boolean mError;
@SerializedName("contents")
@Expose
private List<Content> mContents;
}
public class Content {
@SerializedName("id")
@Expose
private int mId;
@SerializedName("channel")
@Expose
private Channel mChannel;
@SerializedName("publisher")
@Expose
private Publisher mPublisher;
@SerializedName("title")
@Expose
private String mTitle;
@SerializedName("description")
@Expose
private String mDescription;
@SerializedName("subscribed")
@Expose
private boolean mSubscribed;
public int getId() {
return mId;
}
public Channel getChannel() {
return mChannel;
}
public String getTitle() {
return mTitle;
}
public String getDescription() {
return mDescription;
}
public boolean isSubscribed() {
return mSubscribed;
}
public Publisher getPublisher() {
return mPublisher;
}
}
这是我的混淆规则
-dontwarn javax.annotation.**
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase
-dontwarn org.codehaus.mojo.animal_sniffer.*
-dontwarn okhttp3.internal.platform.ConscryptPlatform
-keepattributes *Annotation*
Publisher 对象正在调试模式下解析(proguard 已禁用)。当 Release APK 运行时,Publisher 对象为 null(proguard 已启用)。
我真的很研究这个问题。但我不明白问题是什么?有没有人可以帮忙?
编辑:我暂时将所有模型 classes 保留在 Proguard 规则中,但这里有一个神秘的错误。我找不到它。为什么 Channel 对象序列化而 Publisher 对象没有序列化?
在你的proguard中添加这个
-keep public class com.package.name.models.** { *; }
注意:- com.package.name.models - 这是您的 POJO class 所在的包名称。
就是这样。