2013-07-16 34 views
1

我知道如何使用Android youtube API來使用視頻ID獲取圖像的縮略圖,但是如何通過使用視頻標題找到視頻的縮略圖?如何通過使用youtube API for android搜索主題來查找YouTube上的視頻鏈接?

+0

剛纔看了[的Youtube API文檔(https://developers.google.com/youtube/2.0/reference)好,你將可以使用'媒體thumbnail' – mutiemule

+0

其實我初學者和實現我已閱讀它,但找不到任何方法來做到這一點。 – Mursaleen

回答

0

要解析的YouTube搜索到的視頻JSON提要使用此鏈接:
https://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=my+love+by+shovon

Here "my love by shovon" is searched item.

使用任何JSON解析器解析它,並得到搜索項的YouTube影片。 使用此Json解析器。

public JSONObject getJson() 
{ 
    DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); 
    HttpPost httppost = new HttpPost("https://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=my%20love%20by%20shovon"); 
    // Depends on your web service 
    httppost.setHeader("Content-type", "application/json"); 

    InputStream inputStream = null; 
    String result = null; 
    HttpResponse response = null; 
    try { 
     response = httpclient.execute(httppost); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }   
    HttpEntity entity = response.getEntity(); 

    try { 
     inputStream = entity.getContent(); 
    } catch (IllegalStateException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    // json is UTF-8 by default i beleive 
    BufferedReader reader = null; 
    try { 
     reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) 
     { 
      sb.append(line + "\n"); 
     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    result = sb.toString(); 
    JSONObject job = null; 
    try { 
     job = new JSONObject(sb.toString()); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return job; 
} 
+0

你能解釋一下如何處理這個問題嗎? – Mursaleen

+0

我編輯了我的答案。請檢查並使用您的應用程序的這個json對象。快樂的編碼。 – Mahmudul

+0

如果這個答案解決你的問題,請讓這個正確的答案:) 快樂的編碼。 – Mahmudul

相關問題