Java URL Build queryStringify(HashMap qString)

Here you can find the source of queryStringify(HashMap qString)

Description

Converts a Map into an HTTP query string

License

Open Source License

Parameter

Parameter Description
qString the map to convert

Return

the converted query string

Declaration

private static String queryStringify(HashMap<String, String> qString) 

Method Source Code


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

import java.io.*;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class Main {
    /**/*from   w  w  w  .ja v a2 s.c  om*/
     * Converts a Map into an HTTP query string
     * @param qString the map to convert
     * @return the converted query string
     */
    private static String queryStringify(HashMap<String, String> qString) {
        String retval = "";
        for (Map.Entry<String, String> pair : qString.entrySet()) {
            try {
                retval += URLEncoder.encode(pair.getKey(), "UTF-8") + "="
                        + URLEncoder.encode(pair.getValue(), "UTF-8");
                retval += "&";
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException("Apparently Unicode isn't a good encoding");
            }
        }
        // Remove last extra ampersand
        retval = retval.substring(0, retval.length() - 1);
        return retval;
    }
}

Related

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