21

What I'm looking for specifically is some code in Java that will take a Map object and convert it into a query string that I can append to a URL I return. I'm sure there's a library that does this and much more, but I can't find it with a quick Google search. Anyone know of one that will do this?

Bialecki
  • 30,061
  • 36
  • 87
  • 109
  • 1
    I had the same question: http://stackoverflow.com/questions/1405731/is-there-a-java-method-that-encodes-a-collection-of-parameters-as-a-url-query-com . No good answer though. :-/ – Steven Huwig Dec 07 '09 at 19:40
  • See also http://www.leveluplunch.com/java/examples/construct-build-uri/ and https://www.quora.com/Is-there-an-open-source-URL-manipulation-library-in-Java. – Vadzim Mar 11 '16 at 17:01
  • Possible duplicate of [What is the idiomatic way to compose a URL or URI in Java?](http://stackoverflow.com/questions/883136/what-is-the-idiomatic-way-to-compose-a-url-or-uri-in-java) – Nick Grealy Apr 13 '17 at 05:03

3 Answers3

12

I found apache httpcomponents to be a solid and versatile library for dealing with HTTP in Java. However, here's a sample class, which might suffice for building URL query strings:

import java.net.URLEncoder;

public class QueryString {

    private String query = "";

    public QueryString(HashMap<String, String> map) {
        Iterator it = mp.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry)it.next();
            query += URLEncoder.encode(pairs.getKey(), "utf-8") + "=" +
            URLEncoder.encode(pairs.getValue(), "utf-8");
            if (it.hasNext()) { query += "&"; }
        }
    }

    public QueryString(Object name, Object value) {
        query = URLEncoder.encode(name.toString(), "utf-8") + "=" +
            URLEncoder.encode(value.toString(), "utf-8");
    }

    public QueryString() { query = ""; }

    public synchronized void add(Object name, Object value) {
        if (!query.trim().equals("")) query += "&";
        query += URLEncoder.encode(name.toString(), "utf-8") + "=" +
            URLEncoder.encode(value.toString(), "utf-8");
    }

    public String toString() { return query; }
}

Usage:

HashMap<String, String> map = new HashMap<String, String>();
map.put("hello", "world");
map.put("lang", "en");

QueryString q = new QueryString(map);
System.out.println(q);
// => "hello=world&lang=en"
miku
  • 181,842
  • 47
  • 306
  • 310
  • Looks good! However, I strongly recommend that you use generics in your code! – notnoop Dec 07 '09 at 18:44
  • I'm going to accept this, but yah the URLEncoder.encode(String s) method is deprecated in favor of one that takes a format argument as well. Other than that, this is great, thanks. – Bialecki Dec 08 '09 at 01:27
  • @miku is there a class in HttpComponents that does what your code does? – Sled Oct 17 '12 at 19:45
6

Try URIBuilder from Apache Http Compoments (HttpClient 4).

It does not actually take a map, but is well suited for building URIs.

Thraidh
  • 670
  • 5
  • 11
1

There's this online, so you can simply call any of:

InputStream serverInput = post(URL url, Map parameters); 
InputStream serverInput = post(URL url, Map parameters); 
InputStream serverInput = post(URL url, Map cookies, Map parameters); 
InputStream serverInput = post(URL url, String[] cookies, Object[] parameters); 
InputStream serverInput = post(URL url, Object[] parameters).

He provides the source code too.

Reverend Gonzo
  • 39,701
  • 6
  • 59
  • 77