4

我在 android 上使用 apache.http 并且大部分工作正常但是当我尝试使用参数执行获取请求时,这些也不会被发送。我究竟做错了什么?这是代码

HttpClient httpclient = new DefaultHttpClient();
HttpUriRequest request = new HttpGet(uri);

HttpParams p = new BasicHttpParams();
p.setParameter("param", "value");
request.setParams(p);

request.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");

HttpResponse response = null;

try {
    response = httpclient.execute(request);
} catch (ClientProtocolException e) {
    e.printStackTrace();
}

在服务器上我只得到这个没有参数

GET /api/v1/activity/ HTTP/1.1
Host: example.com
Connection: Keep-Alive
Accept-Encoding: gzip
Accept: application/json, text/javascript, */*; q=0.01

有任何想法吗?

4

1 回答 1

3

您将不得不修改 URI,如下所示:

HttpClient httpclient = new DefaultHttpClient();

String url = "http://example.com";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add( new BasicNameValuePair( "param", "value" ) );
URI uri = new URI( url + "?" + URLEncodedUtils.format( params, "utf-8" );

HttpUriRequest request = new HttpGet(uri);

request.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");

HttpResponse response = null;

try {
    response = httpclient.execute(request);
} catch (ClientProtocolException e) {
    e.printStackTrace();
}
于 2013-03-07T21:57:53.603 回答