0

我最近了解了 google 提供的 YouTube API。我正在创建一个 Android 应用程序,它允许其用户通过该应用程序从您的 YouTube 上观看视频(或多个视频)。

到目前为止,在这个在线教程的帮助下;我已将网络服务集成到应用程序中,我试图通过该应用程序从特定的 YouTube 帐户/频道获取视频上传列表。到目前为止它运行良好,但每当我启动应用程序并单击按钮以从 YouTube 获取数据时,它都会返回一个空列表。

下面是与 YouTube 公共 API 交互的类。我已经包含了其他类来帮助实现目标,但是看到返回的是一个空列表,我猜我可能在这里的某个地方错过/出错了......或者可能没有。

private static final String YouTubeVideoUrl = 
        "https://gdata.youtube.com/feeds/api/users/Patrick%20Tolani/uploads?v=2&alt=jsonc";
private static final String logTag = "OpenHeavenReflection";
private static byte[] buff = new byte[1024];
private static final int http_is_ok = 200;

public static class ApiException extends Exception{
    private static final long serialVersionUID = 1L;

    public ApiException(String msg){
        super (msg);
    }

    public ApiException(String msg, Throwable thr){
        super(msg, thr);
    }
}

/*
 * Download most recent uploads from a particular YouTube account user
 * @params
 * @return An array of json strings returned by the YouTube API
 * @throws ApiException
 */
protected static synchronized String getVideosFromServer(String... params) throws ApiException{

    String url = YouTubeVideoUrl;
    String returnVal = null;

    Log.d(logTag,"Fetching " + url);

    //Here I'll now create a HTTP client and a request object.
    HttpClient client = new  DefaultHttpClient();
    HttpGet request = new HttpGet();

    try {
        //Here I'll execute the request
        HttpResponse response = client.execute(request);
        StatusLine status = response.getStatusLine();
        if(status.getStatusCode() != http_is_ok){
            throw new ApiException("Invalid response from YouTube" + 
                    status.toString());
        }

        //Here I'll process the content.
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        ByteArrayOutputStream content = new ByteArrayOutputStream();
        int countRead = 0;
        while((countRead = is.read(buff)) != -1){
            content.write(buff, 0, countRead);
        }
        /*this returns a string of the recent uploads 
         * by the specified YouTube account.
         */
        returnVal = new String(content.toByteArray());
    }
    catch(Exception e){
        throw new ApiException("Problem connecting to Server" + e.getMessage(), e);
    }
    return returnVal;
}

你能帮我理解为什么会得到一个空列表吗?

4

0 回答 0