2013-03-07 85 views
4

我在android上使用apache.http,大部分工作正常,但是當我嘗試使用參數執行get請求時,這些都沒有被髮送。我究竟做錯了什麼?這裏是只得到這個沒有PARAMSHttpGet params沒有被髮送

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

任何想法的代碼

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(); 
} 

在服務器上的IM?

回答

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(); 
} 
+0

這個偉大的工程,我使用它,但現在根據每個人的setParams方法應該做的伎倆。這很古怪。 – manuel 2013-03-09 03:56:51

+0

看起來應該是這樣,但我從來沒有見過使用'setParams()'的例子。即使Apache的官方示例也使用URIBuilder的setParameter方法在URI中構建參數。 – 323go 2013-03-10 02:32:34