Gson deserialize null pointer in released apk

后端 未结 2 1272
深忆病人
深忆病人 2020-12-28 14:30

I\'m writing an Android app need using gson to deserialize the json string:

{
    \"reply_code\": 001,
    \"userinfo\": {
        \"username\": \"002\",
            


        
2条回答
  •  轮回少年
    2020-12-28 14:57

    You have ProGuard enabled in your release build type - minifyEnabled true. It obfuscates the code by changing class/variable names.

    You should annotate your class properties, so Gson knows what to look for:

    public class ReturnData {
        @SerializedName("reply_code")
        public String reply_code;
        @SerializedName("userinfo")
        public userinfo userinfo;
    }
    
    public class userinfo {
        @SerializedName("username")
        public String username;
        @SerializedName("userip")
        public String userip;
    }
    

    This way Gson won't look at the properties' names, but will look at @SerializedName annotation.

提交回复
热议问题