Java URI Create addParameter(String name, String value, StringBuilder uri)

Here you can find the source of addParameter(String name, String value, StringBuilder uri)

Description

add Parameter

License

Apache License

Declaration

public static void addParameter(String name, String value, StringBuilder uri) 

Method Source Code

//package com.java2s;
/**//w  w w  .  ja v a2 s.  c  om
 * Copyright 2012-, Cloudsmith Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

public class Main {
    private static final String QUERY_ENCODING = "UTF-8";

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

    /**
     * Encode a value using the URLEncoder and UTF-8
     * 
     * @param value The value to encode
     * @return The encoded value.
     */
    public static String encode(String value) {
        try {
            return URLEncoder.encode(value, QUERY_ENCODING);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(e);
        }
    }
}

Related

  1. addParameters(Map parameters, StringBuilder uri)
  2. addParams(final Map params, final StringBuilder uri)
  3. addParams(URI uri, Map> params, Set overridenParams)
  4. addQueryParametersToUri(URI uri, Map parameters)