尝试从youtube获取用户最喜欢的视频列表

时间:2013-11-28 11:31:08

标签: android youtube-api httpurlconnection urlconnection youtube-data-api

尝试从youtube获取收藏的视频列表,但始终获得

  

java.io.FileNotFoundException

下面是我的代码

String qurl = "https://gdata.youtube.com/feeds/api/users/default/favorites?alt=json&v=2&access_token=xxxxxxxxxxxx" ;

DownloadTask task = new DownloadTask();
task.execute(qurl);




public class DownloadTask extends AsyncTask<String , Void, Void>
{

        @Override
        protected Void doInBackground(String... url) {
            String playlistFetchUrl = url[0];
            try {
                String data = downloadUrl(playlistFetchUrl);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
    }

}


private String downloadUrl(String StrUrl) throws IOException{
    HttpURLConnection urlConnection = null;
    InputStream isStream = null ;
    String data = "";
    try {
        URL url = new URL(StrUrl);

        // Creating an http connection to communicate with url 
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();
        isStream = urlConnection.getInputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(isStream));

        StringBuffer sb  = new StringBuffer();

        String line = "";
        while( ( line = br.readLine())  != null){
            sb.append(line);
        }

        data = sb.toString();

        br.close();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    catch(IOException e){
        e.printStackTrace();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    finally{
        isStream.close();
        urlConnection.disconnect();
    }
    return data ;
}

和logcat是:

 W/System.err(15212): java.io.FileNotFoundException: https://gdata.youtube.com/feeds/api/users/default/favorites?alt=json&v=2&access_token=xxxxxxxxxxxxxxxxxxxxx

 W/System.err(15212):   at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)

W/System.err(15212):    at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246)

任何人都可以回答我为什么会发生这种异常吗?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

经过大量的谷歌搜索和尝试,我在客户端库的帮助下完成了这项工作,这对我有用.... https://developers.google.com/youtube/2.0/developers_guide_java#Retrieving_Favorite_Videos