2

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);
Devrath
  • 42,072
  • 54
  • 195
  • 297

4 Answers4

6

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");
atok
  • 5,880
  • 3
  • 33
  • 62
3

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);
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

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;
}
Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19
0

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"];

Abdulhakim Zeinu
  • 3,333
  • 1
  • 30
  • 37