I'm new to Java and programming for Android and I've seen a lot of tutorials but I am kinda clueless atm on how to loop through a JSONObject and set it to my class.
Class I made:
public Episode(JSONObject obj) {
try {
this.id = Integer.parseInt(obj.getString("episode").toString());
this.tvId = Integer.parseInt(obj.getString("tvdbid").toString());
this.resource = obj.getString("resource").toString();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
I came as far as this...
ArrayList episodeList = new ArrayList();
JSONObject data = new JSONObject();
for(int i = 0; i < 2; i++) {
try {
data = response.getJSONObject("data");
episodeList.add(new Episode(data));
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
// for each entry create new episode :)
} else {
return null;
}
解决方案
Found it :)
try {
response = new JSONObject(con.query("history", parameters));
JSONArray data = response.getJSONArray("data");
for(int i = 0; i < data.length(); i++) {
try {
episodeList.add(new Episode((JSONObject) data.get(i)));
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}