Java URL Parameter Builder format(Map parameters, final String encoding)

Here you can find the source of format(Map parameters, final String encoding)

Description

format

License

Open Source License

Declaration

public static String format(Map<String, String> parameters, final String encoding)
            throws UnsupportedEncodingException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Whizzo Software, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

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

public class Main {
    private static final String PARAMETER_SEPARATOR = "&";
    private static final String KEY_VALUE_SEPARATOR = "=";
    private static final String DEFAULT_ENCODING = "ISO-8859-1";

    public static String format(Map<String, String> parameters, final String encoding)
            throws UnsupportedEncodingException {
        final StringBuilder result = new StringBuilder();
        for (String key : parameters.keySet()) {
            final String encodedKey = URLEncoder.encode(key, (encoding != null ? encoding : DEFAULT_ENCODING));
            final String value = parameters.get(key);
            final String encodedValue;
            if (value != null) {
                encodedValue = URLEncoder.encode(value, encoding != null ? encoding : DEFAULT_ENCODING);
            } else {
                encodedValue = "";
            }/* ww w.j a v a 2  s  .com*/
            if (result.length() > 0) {
                result.append(PARAMETER_SEPARATOR);
            }
            result.append(encodedKey);
            result.append(KEY_VALUE_SEPARATOR);
            result.append(encodedValue);
        }
        return result.toString();
    }
}

Related

  1. convertHttpParameters(Map parameters)
  2. convertToDataString(Map data)
  3. convertToParameterString(Map parameterMap)
  4. extractParameters(String parameterString)
  5. format(final Map> parameters, final String encoding)
  6. getParameter(String source, int index)
  7. getParameterMap(byte[] bytea)
  8. getParams(String str)
  9. getUrlParameters(final String url)