Java URL Build buildQueryString(Map parameters)

Here you can find the source of buildQueryString(Map parameters)

Description

Builds a query string from the provided key-value-pairs.

License

Open Source License

Declaration

private static String buildQueryString(Map parameters) throws UnsupportedEncodingException 

Method Source Code


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

import java.io.*;

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

public class Main {
    /**//from w  ww .java2s  . com
     * Builds a query string from the provided key-value-pairs. All
     * spaces are substituted by '+' characters, and all non US-ASCII
     * characters are escaped to hexadecimal notation (%xx).
     */
    private static String buildQueryString(Map parameters) throws UnsupportedEncodingException {
        StringBuffer queryString = new StringBuffer(20 * parameters.size());
        for (Iterator it = parameters.entrySet().iterator(); it.hasNext();) {
            Map.Entry parameter = (Map.Entry) it.next();
            // Escape both key and value and combine them with an '='
            queryString.append(URLEncoder.encode((String) parameter.getKey(), "UTF-8"));
            queryString.append('=');
            queryString.append(URLEncoder.encode(String.valueOf(parameter.getValue()), "UTF-8"));
            if (it.hasNext())
                queryString.append('&');
        }
        return queryString.toString();
    }
}

Related

  1. buildQuery(final Map query)
  2. buildQuery(Map paramMap)
  3. buildQuery(Map paramMap)
  4. buildQuery(Map query)
  5. buildQuery(Map params)
  6. buildQueryString(Map params)
  7. buildQueryString(Map queryParams)
  8. buildQueryString(Map params)
  9. buildQueryString(Multimap params)