Java URL Build queryStringFromMap(Map map)

Here you can find the source of queryStringFromMap(Map map)

Description

query String From Map

License

Open Source License

Declaration

public static String queryStringFromMap(Map<String, String> map) throws UnsupportedEncodingException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import java.util.Iterator;
import java.util.Map;

public class Main {
    public static String queryStringFromMap(Map<String, String> map) throws UnsupportedEncodingException {
        StringBuilder encodedData = new StringBuilder();
        Iterator<Map.Entry<String, String>> mapIterator = map.entrySet().iterator();
        while (mapIterator.hasNext()) {
            Map.Entry<String, String> entry = mapIterator.next();
            if (mapIterator.hasNext()) {
                encodedData.append(String.format("%s&", urlEncodeEntry(entry)));
            } else {
                encodedData.append(urlEncodeEntry(entry));
            }/*w w  w.  ja v  a  2  s. c  o m*/
        }
        return encodedData.toString();
    }

    public static String urlEncodeEntry(Map.Entry<String, String> entry) throws UnsupportedEncodingException {
        String encodedEntry = String.format("%s=%s", URLEncoder.encode(entry.getKey(), "utf-8"),
                URLEncoder.encode(entry.getValue(), "utf-8"));
        return encodedEntry;
    }
}

Related

  1. buildURLForPath(String[] _path)
  2. buildUrlFromParts(String... parts)
  3. buildUrlWithFilter(String url, String[] filterArrayString)
  4. queryString(final Map> params)
  5. queryString(final Map values)
  6. queryStringify(HashMap qString)
  7. queryStringToMap(String query, Boolean decode)
  8. queryStringToMap(String queryString)
  9. queryStringToMap(String queryString, String charSet)