2017-07-19 196 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. 在你的接口中调用该方法,并检索回调中的反序列化对象。

祝你好运!