Java URI Create addParams(final Map params, final StringBuilder uri)

Here you can find the source of addParams(final Map params, final StringBuilder uri)

Description

add Params

License

Open Source License

Declaration

public static void addParams(final Map<String, String> params, final StringBuilder uri) 

Method Source Code

//package com.java2s;
/******************************************************************************
 *  Copyright (c) 2011 GitHub Inc.// w  ww  .  j  a  v  a  2s .c om
 *  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
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *****************************************************************************/

import java.io.UnsupportedEncodingException;

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

public class Main {
    public static void addParams(final Map<String, String> params, final StringBuilder uri) {
        if (params == null || params.isEmpty())
            return;
        for (Map.Entry<String, String> param : params.entrySet())
            addParam(param.getKey(), param.getValue(), uri);
    }

    public static void addParam(final String name, final String value, final StringBuilder uri) {
        if (uri.length() > 0)
            uri.append('&');
        uri.append(encode(name)).append('=');
        if (value != null)
            uri.append(encode(value));
    }

    public static String encode(final String value) {
        try {
            return URLEncoder.encode(value, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(e);
        }
    }
}

Related

  1. addParameter(String name, String value, StringBuilder uri)
  2. addParameters(Map parameters, StringBuilder uri)
  3. addParams(URI uri, Map> params, Set overridenParams)
  4. addQueryParametersToUri(URI uri, Map parameters)
  5. addQueryParamsToUri(String uri, Map queryParams)
  6. addQueryString(final URI uri, final String queryString)