Android HttpPost get“;无效的请求URI“;来自Youtube

Android HttpPost get“;无效的请求URI“;来自Youtube,android,youtube,http-post,Android,Youtube,Http Post,我正在尝试访问Youtube的URL(手机没有代理设置,直接连接到互联网) 即使我没有登录Youtube,上面我放在任何PC浏览器中的URL都工作正常(将返回纯JSON字符串) 但是我试图通过Android的HttpPost访问的这个URL,我只能得到无效的请求URI 这是我的密码: public static final String HEADER_TYPE="Content-Type"; private static final String HEADER_TYPE_SET

我正在尝试访问Youtube的URL(手机没有代理设置,直接连接到互联网)

即使我没有登录Youtube,上面我放在任何PC浏览器中的URL都工作正常(将返回纯JSON字符串)

但是我试图通过Android的HttpPost访问的这个URL,我只能得到无效的请求URI

这是我的密码:

    public static final String HEADER_TYPE="Content-Type";
    private static final String HEADER_TYPE_SET="application/text; charset=utf-8";
    public static final String HEADER_TYPE_2="Accept";
    private static final String HEADER_TYPE_SET_2="application/x-www-form-urlencoded";

    /** Socket操作逾時(3600秒) */
    public static final int TIMEOUT_SOCKET=7000;
    /** 連線逾時(3600秒) */
    public static final int TIMEOUT_CONECTION=7000;

    List<NameValuePair> params=new ArrayList<NameValuePair>();
    HttpParams httpP=null;
    DefaultHttpClient mHttpClient=null;
    UrlEncodedFormEntity entity=null;
    HttpResponse httpResp=null;
    String ret="";

    BasicHeader[] header=new BasicHeader[2];
    header[0]=new BasicHeader(HEADER_TYPE, HEADER_TYPE_SET);
    header[1]=new BasicHeader(HEADER_TYPE_2, HEADER_TYPE_SET_2);
    httpP=new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpP, TIMEOUT_CONECTION);
    HttpConnectionParams.setSoTimeout(httpP, TIMEOUT_SOCKET);
    String url=new String("http://gdata.youtube.com/feeds/api/videos/-/music?q=gagamv&max-results=50&alt=json&v=2");
    Log.d(Constants.TAG, "Youtube search URL: "+url);
    HttpPost post=new HttpPost(url);
    mHttpClient=new DefaultHttpClient(httpP);

    entity=new UrlEncodedFormEntity(params);
    post.addHeader(entity.getContentType());
    post.setEntity(entity);
    httpResp=mHttpClient.execute(post);

    String status=httpResp.getStatusLine().toString();
    ret=new String(EntityUtils.toString(httpResp.getEntity(), "UTF-8"));
publicstaticfinalstringheader\u TYPE=“contenttype”;
私有静态最终字符串头\u TYPE\u SET=“application/text;charset=utf-8”;
公共静态最终字符串头\u TYPE\u 2=“接受”;
私有静态最终字符串头\u TYPE\u SET\u 2=“application/x-www-form-urlencoded”;
/**插座操作逾時(3600秒) */
公共静态最终int超时_套接字=7000;
/** 連線逾時(3600秒) */
公共静态最终整数超时连接=7000;
List params=new ArrayList();
HttpParams httpP=null;
DefaultHttpClient mHttpClient=null;
UrlEncodedFormEntity实体=null;
HttpResponse httpResp=null;
字符串ret=“”;
BasicHeader[]标头=新BasicHeader[2];
表头[0]=新的基本表头(表头类型、表头类型集);
标头[1]=新的基本标头(标头类型\u 2,标头类型\u集\u 2);
httpP=新的BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpP,超时连接);
HttpConnectionParams.setSoTimeout(httpP,超时\u套接字);
字符串url=新字符串(“http://gdata.youtube.com/feeds/api/videos/-/music?q=gagamv&max-结果=50&alt=json&v=2“;
Log.d(Constants.TAG,“Youtube搜索URL:+URL”);
HttpPost=新的HttpPost(url);
mHttpClient=新的默认HttpClient(httpP);
实体=新的UrlEncodedFormEntity(参数);
post.addHeader(entity.getContentType());
后设实体(实体);
httpResp=mHttpClient.execute(post);
字符串状态=httpResp.getStatusLine().toString();
ret=新字符串(EntityUtils.toString(httpResp.getEntity(),“UTF-8”);

如何克服此问题?

您需要使用HTTP GET请求。问题是您使用的是HTTP Post,使用GET请求时url将返回数据

尝试以下代码(我已将缓冲区大小设置为4096,但您可以根据需要将其更改为动态):


你为什么要用POST?你知道你没有在params中放任何东西吗?哎呀,我又在一个愚蠢的情况下发了几个小时的垃圾邮件…谢谢你的小费伙计:-)我的代码的另一部分正在用HttpPost连接我的服务器,我只是把代码复制到Youtube上,忘了改成HttpGet…谢谢:-)
        URL url = new URL("http://gdata.youtube.com/feeds/api/videos/-/music?q=gagamv&max-results=50&alt=json&v=2");
           HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
           try {
             InputStream in = new BufferedInputStream(urlConnection.getInputStream());
             byte buffer[] = new byte[4096];

             in.read(buffer);
             String str = new String(buffer);
             Log.d(TAG, " Data: " + str);

           } finally {
             urlConnection.disconnect();
           }