Android HTTP GET parameters

16,161

Solution 1

unlike POST, GET sends the parameters under the url like this:

http://myurl.com?variable1=value&variable2=value2

Where: the parameters area start from the question mark and on so the variable1 is the first param and it has "value" value...

See here for more informations.

So what you need to do is just build an url that contains also these parameters according to server needs.

EDIT:

In your case :

HttpGet get = new HttpGet("https://server.com/stuff?count=5&length=5");
...

Where: count=5 and length=5 are the parameters and the "?" mark is the beginning of the parameters definition... I hope that helps.

Solution 2

String url = "https://server.com/stuff"
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("count", "5"));
HttpClient httpClient = new DefaultHttpClient();
String paramsString = URLEncodedUtils.format(nameValuePairs, "UTF-8");
HttpGet httpGet = new HttpGet(url + "?" + paramsString);
HttpResponse response = httpClient.execute(httpGet);

EDIT: Since Android SDK v22, the type NameValuePair is deprecated. I recommend using Volley, an HTTP library that makes networking for Android apps easier and most importantly, faster.

Share:
16,161
mpatten
Author by

mpatten

Updated on June 04, 2022

Comments

  • mpatten
    mpatten almost 2 years

    I'm using the apache http library and need to know how to add a parameter to an HTTP GET request. I've looked over How to add parameters to a HTTP GET request in Android? but the accepted answer for that adds parameters to an HTTP POST. This is my code so far but it is not working.

    HttpGet get = new HttpGet("https://server.com/stuff");
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("count", "5"));
    HttpParams p = get.getParams();
    p.setParameter("length", "5");
    get.setParams(p);