How to Add Parameters to a Http Get Request in Android

How to add parameters to a HTTP GET request in Android?

I use a List of NameValuePair and URLEncodedUtils to create the url string I want.

protected String addLocationToUrl(String url){
if(!url.endsWith("?"))
url += "?";

List<NameValuePair> params = new LinkedList<NameValuePair>();

if (lat != 0.0 && lon != 0.0){
params.add(new BasicNameValuePair("lat", String.valueOf(lat)));
params.add(new BasicNameValuePair("lon", String.valueOf(lon)));
}

if (address != null && address.getPostalCode() != null)
params.add(new BasicNameValuePair("postalCode", address.getPostalCode()));
if (address != null && address.getCountryCode() != null)
params.add(new BasicNameValuePair("country",address.getCountryCode()));

params.add(new BasicNameValuePair("user", agent.uniqueId));

String paramString = URLEncodedUtils.format(params, "utf-8");

url += paramString;
return url;
}

Why we add Query parameters in Get request?

It is not necessary to send Query Parameters with GET requests. It is something related to how the end point is configured on the API you are trying to consume.

While designing APIs especially GET methods certain parameters can be kept optional by specifying them as query parameters.

@GET("location")
Response getUser(@QueryParam("name") String name);

can be called by both

/location

/location?name=test

Query Parameter is not merely confined to GET requests. It can be used with other methods too e.g., DELETE, etc.

This is a concept related to HTTP methods

Retrofit and GET using parameters

AFAIK, {...} can only be used as a path, not inside a query-param. Try this instead:

public interface FooService {    

@GET("/maps/api/geocode/json?sensor=false")
void getPositionByZip(@Query("address") String address, Callback<String> cb);
}

If you have an unknown amount of parameters to pass, you can use do something like this:

public interface FooService {    

@GET("/maps/api/geocode/json")
@FormUrlEncoded
void getPositionByZip(@FieldMap Map<String, String> params, Callback<String> cb);
}

Android HTTP GET parameters

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.

How to add query parameters to a HTTP GET request by OkHttp?

As mentioned in the other answer, okhttp v2.4 offers new functionality that does make this possible.

See http://square.github.io/okhttp/2.x/okhttp/com/squareup/okhttp/HttpUrl.Builder.html#addQueryParameter-java.lang.String-java.lang.String-



This is not possible with the current version of okhttp, there is no method provided that will handle this for you.

The next best thing is building an url string or an URL object (found in java.net.URL) with the query included yourself, and pass that to the request builder of okhttp.

Sample Image

As you can see, the Request.Builder can take either a String or an URL.

Examples on how to build an url can be found at What is the idiomatic way to compose a URL or URI in Java?

How to make an HTTP get request with parameters

In a GET request, you pass parameters as part of the query string.

string url = "http://somesite.com?var=12345";

How to add parameters in android http POST?


How to make an http POST and adding parameters.

how to add parameters? you must have something like.

// Add your data  
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("userid", "12312"));
nameValuePairs.add(new BasicNameValuePair("sessionid", "234"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

This is a complete method:

public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/myexample.php");

try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "stackoverflow.com is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}


Related Topics



Leave a reply



Submit