【发布时间】:2017-08-12 11:29:50
【问题描述】:
JSON:
{
"videos":[
[
{
"video_id":"DKOLynNhWxo",
"video_url":"https:\/\/www.youtube.com\/watch?v=DKOLynNhWxo",
"video_host":"youtube",
"created_at":"2017-08-08 11:17:00",
"branch_name":"Computer Science",
"semester_name":"Semester 1",
"subject_name":"English"
},
{
"video_id":"haYm5k6h5yc",
"video_url":"https:\/\/www.youtube.com\/watch?v=haYm5k6h5yc",
"video_host":"Youtube",
"created_at":"2017-08-10 10:05:00",
"branch_name":"Computer Science",
"semester_name":"Semester 1",
"subject_name":"English"
}
],
[
{
"video_id":"VSkRU8eXFII",
"video_url":"https:\/\/www.youtube.com\/watch?v=VSkRU8eXFII",
"video_host":"youtube",
"created_at":"2017-08-08 11:18:00",
"branch_name":"Computer Science",
"semester_name":"Semester 1",
"subject_name":"Maths"
}
],
[],
[]
]
}
代码:
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(JsonExp.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
AppController sh = new AppController();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("videos");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
//JSONObject cjsnobj = contacts.getJSONObject(i);
JSONArray c = contacts.getJSONArray(i);
for (int y = 0; y < c.length(); y++)
{
JSONObject obj=c.getJSONObject(i);
String aid = obj.getString("video_id");
String url = obj.getString("video_url");
// String name = obj.getString("video_host");
// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("video_id", aid);
contact.put("video_url", url);
// contact.put("video_host", name);
// adding contact to contact list
contactList.add(contact);
}
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
JsonExp.this, contactList,
R.layout.list_item, new String[]{"video_id","video_url"//, "video_host"//, "ProductDetails", "rectimestamp"
}, new int[]{R.id.aid, R.id.name
});
lv.setAdapter(adapter);
}
}
【问题讨论】: