Reputation: 847
I'm using apache.http on android and most of it works fine but when I try to do a get request with parameters, these are nor being sent. What am I doing wrong? Here's the code
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();
}
On the server im getting only this with no 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
Any Ideas?
Upvotes: 4
Views: 3584
Reputation: 14274
You will have to modify the URI, like so:
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();
}
Upvotes: 3