Reputation: 102
For a school project I'm busy writing an Android App for my school which opens the schedules of students. I have them all stored on my webserver and I can open them with domain.com/schedule.php?lln= which opens the schedule for me in JSON format. I've added mine below. So far, so well.
Now that I'm writing the app itself, I run into the limits of my knowledge. I'm trying to get the JSON downloaded and deserialized for use as Strings in the app to fill some Textviews. I've looked around here on how to import the arrays from the url, but nothing really helped me out. I've been messing with Google's GSON a bit, but no luck on there either. Any suggestions?
{"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 ","-","-","-","-","-","-"]]}
I'm pretty new to Android Developing, so much explanation would be appreciated richely!
Upvotes: 0
Views: 185
Reputation: 38635
You should build data model, which describes your response. I see 2 classes: Response
and Info
.
Info class
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 + "]";
}
}
Response class:
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();
}
}
Now, main methods could looks like:
Gson gson = new GsonBuilder().serializeNulls().create();
Response res = gson.fromJson(response, Response.class);
System.out.println(res);
This program prints:
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 , -, -, -, -, -, -]]
Upvotes: 1