1

I followed this other SO question to set parameter for the URL but it was giving error:

The method setQueryString(String) in the type HttpMethodBase is not applicable for the arguments (NameValuePair[])

and

Cannot instantiate the type NameValuePair.

I am not able to understand the actual problem. Could some one help me on this?

The code I have used from the above question

GetMethod method = new GetMethod("example.com/page";); 
method.setQueryString(new NameValuePair[] { 
    new NameValuePair("key", "value") 
}); 
5
  • Did u remove the superfluous semi-colon here "example.com/page"; Commented Apr 26, 2013 at 7:43
  • 2
    Are you using httpclient 3.x or 4.x? The example you give is typical 3.x code, not 4.x. Commented Apr 26, 2013 at 7:43
  • Vikingsteve removed that semi-colon already when excuting the code Commented Apr 26, 2013 at 7:46
  • Great NilsH i am using 4.x.:). How to set parameters when using 4.x ? Commented Apr 26, 2013 at 7:46
  • 2
    @Anto sure? There is no GetMethod class in 4.x version. According to javadoc it is HttpGet Commented Apr 26, 2013 at 7:47

3 Answers 3

14

In HttpClient 4.x, there is no GetMethod anymore. Instead there is HttpGet. Quoting an example from the tutorial:

Query parameters in the url:

HttpGet httpget = new HttpGet(
 "http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=");

Creating the query string programatically:

URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost("www.google.com").setPath("/search")
    .setParameter("q", "httpclient")
    .setParameter("btnG", "Google Search")
    .setParameter("aq", "f")
    .setParameter("oq", "");
URI uri = builder.build();
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());
Sign up to request clarification or add additional context in comments.

2 Comments

I should also add that httpclient 4.x and httpclient 3.x have quite different APIs. Examples you find for httpclient 3.x will most likely not work with 4.x.
I am trying to use HTTPclient 4.2.5 with SalesForce REST API and it does not work - setting parameter results in an error page, while using "setQueryString" with 3.1 API results in JSON... Any clues?
1

Interfaces can not be directly instantiated, you should instantiate classes that implements such Interfaces.

Try this:

NameValuePair[] params = new BasicNameValuePair[] {
        new BasicNameValuePair("param1", param1),
        new BasicNameValuePair("param2", param2),
};

Comments

0

You can pass query parameter within the the url.

String uri = "example.com/page?key=value";
HttpClient httpClient = new DefaultHttpClient();
HttpGet method = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(method);
BufferedReader br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
String content="", line;
while ((line = br.readLine()) != null) {
     content = content + line;
}
System.out.print(content);

2 Comments

There is no GetMethod in the version i have used, Please have a look at the comments i have provided
I changed my answer according to your httpclient 4.x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.