2017-07-19 195 views
0

我需要獲取Youtube視頻的標題,但我似乎無法包裝我的頭如何將JSONObject從方法和字符串中取出。從JSONObject獲取Youtube標題爲一個字符串

public static String getTitleQuietly(String youtubeUrl) { 
    try { 
     if (youtubeUrl != null) { 
      URL embededURL = new URL("http://www.youtube.com/oembed?url=" + 
        youtubeUrl + "&format=json" 
      ); 

      return new JSONObject(IOUtils.toString(embededURL)).getString("title"); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

我試着這樣做:

String x = getTitleQuietly("https://www.youtube.com/watch?v=4fkcTA7YX44"); 
      Toast.makeText(PostingGBDTutorial.this, x, Toast.LENGTH_SHORT).show(); 

切換字符串參數www.youtube.com/watch?v=4fkcTA7YX44,甚至視頻的最後11個字符:4fkcTA7YX44

,但沒有出現。

Where i discovered the code

+0

請參閱https://stackoverflow.com/questions/25228744/how-to-use-oembed-api-using-php-for-youtube –

回答

-1

有用於調用API和JSON響應轉換爲Java對象的工具組合:改造2和jsonschema2pojo。

http://www.jsonschema2pojo.org

http://square.github.io/retrofit/

我不會進入對此處使用它們的細節,但有幾個關鍵步驟...

  1. 讓每一個接口API調用
  2. 使用Retrofit庫實例化接口
  3. 使用jsonschema2po生成pojo對象jo的反序列化
  4. 在你的接口中調用該方法,並檢索回調中的反序列化對象。

祝你好運!