Devrath
Devrath

Reputation: 42854

How to add NameValuePairs for a HttpGet request in android

In the line : httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs));

I am getting error as: The method setEntity(UrlEncodedFormEntity) is undefined for the type HttpGet


CODE:

        HttpClient httpclient = new DefaultHttpClient();
        // Add the header data for the request
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("phonenumber","12345"));
        nameValuePairs.add(new BasicNameValuePair("authtoken", "12345"));
        HttpGet httpget = new HttpGet(url);
        httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs));

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

Upvotes: 2

Views: 8281

Answers (4)

Abdulhakim Zeinu
Abdulhakim Zeinu

Reputation: 3859

the reason is because setEntity method is belongs to HttpPost. Not HttpGet you can correct your error simply by changing HttpGet to HttpPost ..

HttpPost httppost = new HttpPost(url);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

also in your php code change all $_GET["your key"]; methods to $_POST["your key"];

Upvotes: 0

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

Try to use BasicNameValuePair List add params and get formmated string from URLEncodedUtils which concat to url :

HttpClient httpclient = new DefaultHttpClient();
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("phonenumber", "12345"));
params.add(new BasicNameValuePair("authtoken", "12345"));
HttpGet httpget = new HttpGet(url+"?"+URLEncodedUtils.format(params, "utf-8"));
HttpResponse response = httpclient.execute(httpget);

Upvotes: 3

Akash Moradiya
Akash Moradiya

Reputation: 3322

try like this way,

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<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();

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

Upvotes: 0

atok
atok

Reputation: 5948

GET request does not have a body that could contain an entity, the parameters you want to include should be built-in into the URL itself.

A clean way to do this is to use URIBuilder:

URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost(host).setPort(port).setPath(yourpath)
.setParameter("parts", "all")
.setParameter("action", "finish");

Upvotes: 6

Related Questions