本文介绍了Gson在释放的apk中反序列化空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Android应用程序需要使用gson反序列化json字符串:

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

{
    "reply_code": 001,
    "userinfo": {
        "username": "002",
        "userip": 003
    }
}

所以我创建两个类:

public class ReturnData {
    public String reply_code;
    public userinfo userinfo;
}

public class userinfo {
    public String username;
    public String userip;
}

最后,我的MainActivity.java中的Java代码:

finally, my Java code in MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Context context= MainActivity.this;

    //Test JSON
    String JSON="{\"reply_code\": 001,\"userinfo\": {\"username\": \"002\",\"userip\": 003}}";
    Gson gson = new Gson();
    ReturnData returnData=gson.fromJson(JSON,ReturnData.class);

    if(returnData.reply_code==null)
        Toast.makeText(context,"isNULL",Toast.LENGTH_SHORT).show();
    else
        Toast.makeText(context,"notNULL",Toast.LENGTH_SHORT).show();
}

令我困惑的是,当我调试应用程序时,运行良好输出notNULL。我可以看到对象的每个属性已经被反序列化了。
但是,当我从Android Studio生成apk并在手机上运行apk时,输出isNULL,json解析失败!

What made me confused is, when I debug the app,it ran well and output "notNULL".I can see the every attribution of the object has been deserialized properly.However,when I generated released apk from Android Studio and run apk on phone,it output "isNULL",the json resolution failed!

谁能告诉我发生了什么事?

Who can tell me what happened?!

PS:build.gradle:

PS:build.gradle:

apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "19.1"

defaultConfig {
    applicationId "com.padeoe.autoconnect"
    minSdkVersion 14
    targetSdkVersion 21
    versionCode 1
    versionName "2.1.4"
}
buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('src/gson-2.3.1.jar')
}


推荐答案

您的版本中启用了ProGuard 构建类型 - minifyEnabled true 。您可以通过更改类/变量名来模糊代码。

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

您应该注释您的类属性,因此Gson知道要查找的内容:

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;
}

这样,Gson不会查看属性的名称,在 @SerializedName 注释。

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

这篇关于Gson在释放的apk中反序列化空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 20:05