我正在一个项目中使用json从youtube获取视频。 2天前使用此方法
private void showVideo() {
RequestQueue requestQueue= Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest=new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject=new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("items");
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
JSONObject jsonVideoId = jsonObject1.getJSONObject("id");
JSONObject jsonsnippet = jsonObject1.getJSONObject("snippet");
JSONObject jsonObjectdefault = jsonsnippet.getJSONObject("thumbnails").getJSONObject("medium");
VideoDetails videoDetails = new VideoDetails();
String videoid = jsonVideoId.getString("videoId");
Log.e(TAG," New Video Id " + videoid);
videoDetails.setURL(jsonObjectdefault.getString("url"));
videoDetails.setVideoName(jsonsnippet.getString("title"));
videoDetailsArrayList.add(videoDetails);
}
lvVideo.setAdapter(customListAdapter);
customListAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
int socketTimeout = 30000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
requestQueue.add(stringRequest);
}
正常工作,但现在不行了。我在这里发现了一些关于stackoverflow的问题,但是没有一个问题解决了我的问题。我试图改变
JSONObject jsonVideoId = jsonObject1.getJSONObject("id");
String videoid = jsonVideoId.getString("videoId");
到
String videoid = (String) ((JSONObject) ((JSONObject) jsonObject1.getJSONArray("items").get(0)).get("id")).get("videoId");
这些更改显示了视频,但是当我单击某些视频时,只需打开第一个视频(我知道发生这种情况是因为.get(0),但是当我更改为.get(i)时,我会收到一条带有错误信息的消息< / p>
org.json.JSONException: No value for videoId
编辑: 来自YouTube的数据:
{
"kind": "youtube#searchListResponse",
"etag": "\"******"",
"nextPageToken": "******",
"regionCode": "BR",
"pageInfo": {
"totalResults": 76,
"resultsPerPage": 50
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"*****"",
id": {
"kind": "youtube#video",
"videoId": "****"
},