0

对于一个学校项目,我正忙着为我的学校编写一个 Android 应用程序,它可以打开学生的日程安排。我将它们全部存储在我的网络服务器上,我可以使用 domain.com/schedule.php?lln= 打开它们,它会以 JSON 格式为我打开时间表。我在下面添加了我的。到目前为止,一切顺利。

现在我正在编写应用程序本身,我遇到了我的知识极限。我正在尝试将 JSON 下载并反序列化以用作应用程序中的字符串来填充一些 Textview。我在这里查看了如何从 url 导入数组,但没有什么能真正帮助我。我一直在搞乱谷歌的 GSON,但也没有运气。有什么建议么?

{"info":{"niveau": "H5","naam": "Sven Boekelder","lln": "15518","klas": "H5F"},"schedule": [["IN LKH H5.IN1 447 ","ZA H5F ","EN TOO H5F 424 ","SK VEB H5.SK2 411 ","BI ROD H5.BI2 427 ","NA BMF H5.NA2 425 ","LO SCL H5F T56 ","LO SCL H5F T56 ","-"],["M VEB H5F 426 ","BI ROD H5.BI2 427 ","SK VEB H5.SK2 413 ","WB WLH H5.WB1 421 ","NE VRH H5F 404 ","EN TOO H5F 424 ","-","-","-"],["ZB H5F ","NE VRH H5F 404 ","-","IN LKH H5.IN1 453 ","NA BMF H5.NA2 409 ","SK VEB H5.SK2 410 ","-","WB WLH H5.WB1 422 ","-"],["-","EN TOO H5F 424 ","NA BMF H5.NA2 409 ","WB WLH H5.WB1 421 ","-","BI ROD H5.BI2 427 ","-","-","-"],["NE VRH H5F 404 ","-","IN LKH H5.IN1 447 ","-","-","-","-","-","-"]]}

我对Android开发很陌生,如此多的解释将不胜感激!

4

1 回答 1

1

您应该构建描述您的响应的数据模型。我看到 2 个类:ResponseInfo. 信息类

class Info {
    private String niveau;
    private String naam;
    private int lln;
    private String klas;

    public String getNiveau() {
        return niveau;
    }

    public void setNiveau(String niveau) {
        this.niveau = niveau;
    }

    public String getNaam() {
        return naam;
    }

    public void setNaam(String naam) {
        this.naam = naam;
    }

    public int getLln() {
        return lln;
    }

    public void setLln(int lln) {
        this.lln = lln;
    }

    public String getKlas() {
        return klas;
    }

    public void setKlas(String klas) {
        this.klas = klas;
    }

    @Override
    public String toString() {
        return "Info [niveau=" + niveau + ", naam=" + naam + ", lln=" + lln
                + ", klas=" + klas + "]";
    }
}

响应类:

class Response {
    private Info info;
    private String[][] schedule;
public Info getInfo() {
    return info;
}

public void setInfo(Info info) {
    this.info = info;
}

public String[][] getSchedule() {
    return schedule;
}

public void setSchedule(String[][] schedule) {
    this.schedule = schedule;
}

@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("Response [info=");
    builder.append(info).append(", schedule=");
    for (String[] array : schedule) {
        builder.append(Arrays.toString(array));
    }
    builder.append("]");
    return builder.toString();
}

}

现在,主要方法可能如下所示:

Gson gson = new GsonBuilder().serializeNulls().create();
Response res = gson.fromJson(response, Response.class);
System.out.println(res);

该程序打印:

Response [info=Info [niveau=H5, naam=Sven Boekelder, lln=15518, klas=H5F], schedule=[IN LKH H5.IN1 447 , ZA H5F , EN TOO H5F 424 , SK VEB H5.SK2 411 , BI ROD H5.BI2 427 , NA BMF H5.NA2 425 , LO SCL H5F T56 , LO SCL H5F T56 , -][M VEB H5F 426 , BI ROD H5.BI2 427 , SK VEB H5.SK2 413 , WB WLH H5.WB1 421 , NE VRH H5F 404 , EN TOO H5F 424 , -, -, -][ZB H5F , NE VRH H5F 404 , -, IN LKH H5.IN1 453 , NA BMF H5.NA2 409 , SK VEB H5.SK2 410 , -, WB WLH H5.WB1 422 , -][-, EN TOO H5F 424 , NA BMF H5.NA2 409 , WB WLH H5.WB1 421 , -, BI ROD H5.BI2 427 , -, -, -][NE VRH H5F 404 , -, IN LKH H5.IN1 447 , -, -, -, -, -, -]]
于 2013-03-07T19:00:22.400 回答