Java URL Query Build createQueryStringForParameters(Map parameters)

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

Description

create Query String For Parameters

License

Open Source License

Declaration

public static String createQueryStringForParameters(Map<String, String> parameters) 

Method Source Code


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

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

public class Main {
    private static final char PARAMETER_DELIMITER = '&';
    private static final char PARAMETER_EQUALS_CHAR = '=';

    public static String createQueryStringForParameters(Map<String, String> parameters) {
        StringBuilder parametersAsQueryString = new StringBuilder();
        if (parameters != null) {
            boolean firstParameter = true;

            for (String parameterName : parameters.keySet()) {
                if (!firstParameter) {
                    parametersAsQueryString.append(PARAMETER_DELIMITER);
                }/*from w  w w  .ja v  a 2 s . c o  m*/

                parametersAsQueryString.append(parameterName).append(PARAMETER_EQUALS_CHAR)
                        .append(URLEncoder.encode(parameters.get(parameterName)));

                firstParameter = false;
            }
        }
        return parametersAsQueryString.toString();
    }
}

Related

  1. createQueryString(Map options)
  2. createQueryString(Map params)
  3. createQueryString(Map params)
  4. createQueryStringFromMap(Map m, String ampersand, boolean encode)
  5. getQuery(Map params, String encode)
  6. getQuery(URL theURL)
  7. getQueryFromURL(URL url)