小编pad*_*eoe的帖子

Gson在发布的apk中反序列化空指针

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

{
    "reply_code": 001,
    "userinfo": {
        "username": "002",
        "userip": 003
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我创建了两个类:

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

public class userinfo {
    public String username;
    public String userip;
}
Run Code Online (Sandbox Code Playgroud)

最后,我在MainActivity.java中的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();
}
Run Code Online (Sandbox Code Playgroud)

令我困惑的是,当我调试应用程序时,它运行良好并输出"notNULL".我可以看到对象的每个属性都已正确反序列化.但是,当我从Android Studio生成发布的apk并在手机上运行apk时,它输出"isNULL",json分辨率失败!

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

PS:的build.gradle:

apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "19.1" …
Run Code Online (Sandbox Code Playgroud)

debugging android json release gson

19
推荐指数
2
解决办法
3111
查看次数

标签 统计

android ×1

debugging ×1

gson ×1

json ×1

release ×1