2016-08-25 78 views
1

這是我在這裏看到的一個代碼,它使用視頻ID從youtube獲取單個視頻的數據。但是,我似乎無法從URL中獲得「標題」。它在我的瀏覽器中工作,但在我正在開發的android應用程序中,它沒有。代碼有什麼問題?如何從此API提取「標題」?

private class RequestTask extends AsyncTask<String, String, String> { 
    // make a request to the specified url 
    @Override 
    protected String doInBackground(String... uri) { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpResponse response; 
     String responseString = null; 
     try { 
      // make a HTTP request 
      response = httpclient.execute(new HttpGet(uri[0])); 
      StatusLine statusLine = response.getStatusLine(); 
      if (statusLine.getStatusCode() == HttpStatus.SC_OK) { 
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
       response.getEntity().writeTo(out); 
       out.close(); 
       responseString = out.toString(); 
      } else { 
       // close connection 
       response.getEntity().getContent().close(); 
       throw new IOException(statusLine.getReasonPhrase()); 
      } 
     } catch (Exception e) { 
      Log.d("Test", "Couldn't make a successful request!"); 
     } 
     return responseString; 
    } 

    @Override 
    protected void onPostExecute(String response) { 
     super.onPostExecute(response); 

     try { 
      // convert the String response to a JSON object 
      JSONObject jsonResponse = new JSONObject(response); 
      Toast.makeText(getActivity(), jsonResponse.getString("title"), Toast.LENGTH_SHORT).show(); 

      // fetch the array of movies in the response 
      JSONArray jArray = jsonResponse.getJSONArray("movies"); 


     } catch (JSONException e) { 
      Log.d("Test", "Couldn't successfully parse the JSON response!"); 
     } 
    } 
} 

這裏順便API,

https://www.googleapis.com/youtube/v3/videos?id=P3mAtvs5Elc&key=<INSERT_API_SERVER_KEY_HERE>&fields=items(id,snippet(description,channelId,title,categoryId),statistics)&part=snippet,statistics 
+0

而不是'jsonResponse.getString(「title」)'嘗試使用'js onResponse.getJSONObject( 「標題」)。的toString()'。 –

+0

將您的回覆記錄爲日誌。發佈您收到的回覆或異常。 –

回答

1

下面是YouTube的API響應會怎麼看起來像:

{ 
"kind": "youtube#videoListResponse", 
"etag": "\"UCBpFjp2h75_b92t44sqraUcyu0/sDAlsG9NGKfr6v5AlPZKSEZdtqA\"", 
"videos": [ 
    { 
    "id": "7lCDEYXw3mM", 
    "kind": "youtube#video", 
    "etag": "\"UCBpFjp2h75_b92t44sqraUcyu0/iYynQR8AtacsFUwWmrVaw4Smb_Q\"", 
    "snippet": { 
    "publishedAt": "2012-06-20T22:45:24.000Z", 
    "channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw", 
    "title": "Google I/O 101: Q&A On Using Google APIs", 
    "description": "Antonio Fuentes speaks to us and takes questions on working with Google APIs and OAuth 2.0.", 
    "thumbnails": { 
    "default": { 
     "url": "https://i.ytimg.com/vi/7lCDEYXw3mM/default.jpg" 
    }, 
    "medium": { 
     "url": "https://i.ytimg.com/vi/7lCDEYXw3mM/mqdefault.jpg" 
    }, 
    "high": { 
     "url": "https://i.ytimg.com/vi/7lCDEYXw3mM/hqdefault.jpg" 
    } 
    }, 
    "categoryId": "28" 
    }, 
    "contentDetails": { 
    "duration": "PT15M51S", 
    "aspectRatio": "RATIO_16_9" 
    }, 
    "statistics": { 
    "viewCount": "3057", 
    "likeCount": "25", 
    "dislikeCount": "0", 
    "favoriteCount": "17", 
    "commentCount": "12" 
    }, 
    "status": { 
    "uploadStatus": "STATUS_PROCESSED", 
    "privacyStatus": "PRIVACY_PUBLIC" 
    } 
    } 
] 
} 

要獲得下面的代碼的視頻使用標題:

JSONObject responseObject = new JSONObject(response); 
JSONArray videosArray = jsonObject.getJSONArray("videos"); 
JSONObject videoObject = videosArray.getJSONObject(0); //Pointing to first video 
JSONObject snippetObject = videoObject.getJSONObject("snippet"); 
String title = snippetObject.getString("title");