我将json解析到我的Android应用程序时遇到了一些问题
我想获得缩略图URL和videoId,但它显示缩略图JSONException没有值,
videoId发生了同样的情况
我能怎么做?请帮忙
这是我的json文件和片段活动的样子:
请求网址:
GET https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=UC_x5XG1OV2P6uZZ5FSM9Ttw&maxResults=1&key={YOUR_API_KEY}
json回复:
{
"kind": "youtube#playlistListResponse",
"etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/QnDwmfxnHa--I54eAlcSQNwG2Ok\"",
"nextPageToken": "CAEQAA",
"pageInfo": {
"totalResults": 291,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#playlist",
"etag":"\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/IB0XO8J78ybZZvHQq59Q2Y8WoFI\"",
"id": "PLOU2XLYxmsIJ79h65LUPZarLyVmuI-I7b",
"snippet": {
"publishedAt": "2015-07-17T18:59:08.000Z",
"channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
"title": "This week on Google Developers 7/11/15 - 7/17/15",
"description": "",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/ygFe9Pj5N60/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/ygFe9Pj5N60/mqdefault.jpg",
"width": 320,
"height": 180
},
"position": 0,
"resourceId": {
"videoId": "1_hgNNFAfm4"
}
}
}
]
}
片段活动jsonResponse代码:
private void parseJSONReaponse(JSONObject response) {
if (response == null || response.length() == 0) {
return;
}
try {
StringBuilder data = new StringBuilder();
JSONArray arrayItems = response.getJSONArray(KEY_ITEMS);
for (int i = 0; i < arrayItems.length(); i++) {
JSONObject currentItems = arrayItems.getJSONObject(i);
String id = currentItems.getString(KEY_ID);
//data.append(id + "\n");
JSONObject objectSnippet = currentItems.getJSONObject(KEY_SNIPPET);
String title = null;
if (objectSnippet.has(KEY_TITLE)) {
title = objectSnippet.getString(KEY_TITLE);
}
else {
title = "NA";
}
//data.append(id + title + "\n\n");
JSONObject objectThumbnail = objectSnippet.getJSONObject(KEY_THUMBNAIL);
JSONObject objectThumbnailMedium = objectThumbnail.getJSONObject(KEY_THUMBNAIL_MEDIUM);
String urlThumbnail = null;
if (objectThumbnailMedium.has(KEY_THUMBNAIL_MEDIUM)) {
urlThumbnail = objectThumbnailMedium.getString(KEY_THUMBNAIL_MEDIUM_URL);
}
JSONObject objectResourceId = objectSnippet.getJSONObject(KEY_RESOURCEID);
String resourceID = null;
if (objectResourceId.has(KEY_VIDEOID)){
resourceID = objectResourceId.getString(KEY_VIDEOID);
}
VideoData videoData = new VideoData();
videoData.setTitle(title);
videoData.setUrlThumbnail(urlThumbnail);
videoData.setVideoId(resourceID);
listVideos.add(videoData);
data.append(id + title + urlThumbnail + resourceID +"\n\n");
}
L.T(getActivity(),listVideos.toString());
// L.T(getActivity(),data.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
这里是LogCat Msgs
07-18 01:11:25.075 1823-1823/com.example.example W/System.err﹕ org.json.JSONException: No value for thumbnails
07-18 01:11:25.075 1823-1823/com.example.example W/System.err﹕ at org.json.JSONObject.get(JSONObject.java:389)
07-18 01:11:25.075 1823-1823/com.example.example W/System.err﹕ at org.json.JSONObject.getJSONObject(JSONObject.java:609)
07-18 01:11:25.075 1823-1823/com.example.example W/System.err﹕ at com.example.example.fragment.VideoTabFragment.parseJSONReaponse(VideoTabFragment.java:110)
07-18 01:11:25.075 1823-1823/com.example.example W/System.err﹕ at com.example.example.fragment.VideoTabFragment.access$000(VideoTabFragment.java:31)
07-18 01:11:25.075 1823-1823/com.example.example W/System.err﹕ at com.example.example.fragment.VideoTabFragment$1.onResponse(VideoTabFragment.java:75)
07-18 01:11:25.076 1823-1823/com.example.example W/System.err﹕ at com.example.example.fragment.VideoTabFragment$1.onResponse(VideoTabFragment.java:71)
07-18 01:11:25.076 1823-1823/com.example.example W/System.err﹕ at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
07-18 01:11:25.076 1823-1823/com.example.example W/System.err﹕ at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
07-18 01:11:25.076 1823-1823/com.example.example W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
07-18 01:11:25.076 1823-1823/com.example.example W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
07-18 01:11:25.076 1823-1823/com.example.example W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
07-18 01:11:25.076 1823-1823/com.example.example W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5254)
07-18 01:11:25.076 1823-1823/com.example.example W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
07-18 01:11:25.076 1823-1823/com.example.example W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
07-18 01:11:25.076 1823-1823/com.example.example W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-18 01:11:25.076 1823-1823/com.example.example W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)