获取Youtube api的HttpResponse(未找到)

时间:2013-12-27 05:48:54

标签: android youtube-api httpresponse

我在我的应用程序中使用YouTube api。我收到了HttpResposeException。我收到了以下错误:

我的活动:

searching="https://www.googleapis.com/youtube/v3/search?part=snippet&videoSyndicated=true&type=video&q=len&key=AIzaSyDEWZ7vU_zPi1qgaWEREQEJi2FB3IQCE4g&maxResults=25&pageToken=";
protected Long doInBackground(URL... params) 
    {
        //TODO Auto-generated method stub
        result = UrltoValue.getValuefromUrl(searching);
        return null;
    }

logcat的:

org.apache.http.client.HttpResponseException: Not Found
    at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71)
    at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)

将网址转换为响应的类:

public class UrltoValue {
    static String page="zero";
    public static String getValuefromUrl(String url) throws TimeoutException
    {
        try
        {
            final int TIMEOUT_MILLISEC = 20000;
            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
            HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
            HttpConnectionParams.setTcpNoDelay(httpParams, true);
            HttpClient httpClient = new DefaultHttpClient(httpParams);
            //DefaultHttpClient httpClient = new DefaultHttpClient();
            URLEncoder.encode(url, "UTF-8");
            HttpPost httpPost = new HttpPost(url);

            ResponseHandler<String> resHandler = new BasicResponseHandler();
            page = httpClient.execute(httpPost, resHandler);
            Log.v("PAGE",page);
            return page;
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            String zero="zero";
            return zero;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            String zero="zero";
            return zero;
        } catch(IllegalArgumentException e) {
            e.printStackTrace();
            String zero="zero";
            return zero;
            //Toast.makeText(context, "Please remove % symbol", Toast.LENGTH_SHORT);
        } catch(Exception e) {
            e.printStackTrace();
            String zero="zero";
            return zero;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你指的是一个非常特殊的网址。这是你的stacktrace所说的:

org.apache.http.client.HttpResponseException: Not Found

原因是youtube网址不接受POST请求。

改变这个:

HttpPost httpPost = new HttpPost(url);
page = httpClient.execute(httpPost, resHandler);

到此:

HttpGet httpGet = new HttpGet(url);
page = httpClient.execute(httpGet, resHandler);

一切都应该有效。