簡體   English   中英

Gson使用Proguard在Android上解析

[英]Gson parsing on android with proguard

使用proguard時,我只有Gson解析問題。 在沒有proguard的調試模式和發布模式下,一切正常。

這是我的json字符串:

{“ 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字符串的文件,並通過以下方式對其進行了解析:

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()

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 我已經將這些規則包括在內。 知道是什么原因嗎?

正如darwin所說,在我的POJOs類中添加@Keep批注解決了該問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM