Gson在Android上使用Proguard进行解析 [英] Gson parsing on android with proguard

查看:116
本文介绍了Gson在Android上使用Proguard进行解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用proguard时,我只有Gson解析问题.在没有proguard的调试模式和发布模式下,一切正常.

I only have a parsing problem with Gson when I use proguard. In debug mode and release mode without proguard, everything work fine.

这是我的json字符串:

This is my json string :

{表格":[{名称":配置文件",列":[{标签":"id",类型":文本",主要":true},{标签:"名称,"类型:"文本},{"标签:"年龄,"类型:"整数},{"标签:"人类,"类型:"整数} ,{"label":"gear","type":"Gear","custom":true,"list":true}]} ,, {"name":"Gear","columns":[{"label :" id," type:" text," primary:true},{" label:" type," type:" text," enum:true},{" label: "name","type":"text"},{"label":"id_Profile","type":"integer","foreign_key":true}]},{"name":"Animal","columns :[{" label:" id," type:" text," primary:true},{" label:" name," type:" text},{" label: "magic","type":"integer"},{"label":"id_Profile","type":"integer","foreign_key":true}]}]}}

{"tables":[{"name":"Profile","columns":[{"label":"id","type":"text","primary":true},{"label":"name","type":"text"},{"label":"age","type":"integer"},{"label":"human","type":"integer"},{"label":"gear","type":"Gear","custom":true,"list":true}]},{"name":"Gear","columns":[{"label":"id","type":"text","primary":true},{"label":"type","type":"text","enum":true},{"label":"name","type":"text"},{"label":"id_Profile","type":"integer","foreign_key":true}]},{"name":"Animal","columns":[{"label":"id","type":"text","primary":true},{"label":"name","type":"text"},{"label":"magic","type":"integer"},{"label":"id_Profile","type":"integer","foreign_key":true}]}]}

这些是我的POJO:

public class Database {
    private List<Table> tables;

    public List<Table> getTables() {
        return tables;
    }

    public void setTables(List<Table> tables) {
        this.tables = tables;
    }
}

public class Table {
    private String name;
    private List<Column> columns;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Column> getColumns() {
        return columns;
    }

    public void setColumns(List<Column> columns) {
        this.columns = columns;
    }
 }

我读取了包含json字符串的文件,并以这种方式对其进行了解析:

I read the file containing the json string and parse it this way :

public Database getDatabase(Context context) throws IOException {
    InputStream stream = context.getAssets().open(PATH);
    String schema = getJsonSchema(stream);
    Gson gson = new Gson();
    return gson.fromJson(schema, Database.class);
}

但是当我尝试从我的Database对象调用方法getTables()时:

But when I try to call the method getTables() from my Database object :

for (Table table : database.getTables()) {
    //...
}

我收到此错误:java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference.我已经将这些规则包括在内捍卫者.你知道是什么原因吗?

I receive this error : java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference. I have included those rules for proguard. Any idea what's the reason ?

推荐答案

正如达尔文所说,将@Keep批注添加到我的POJOs类中可以解决此问题

As darwin commented, adding the @Keep annotation to my POJOs classes fixed the issue

这篇关于Gson在Android上使用Proguard进行解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆