Java URL Parameter Builder addParameter(Map> map, String pair)

Here you can find the source of addParameter(Map> map, String pair)

Description

add Parameter

License

Open Source License

Declaration

private static void addParameter(Map<String, List<String>> map, String pair) 

Method Source Code


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

import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

public class Main {
    private static void addParameter(Map<String, List<String>> map, String pair) {
        if (pair == null || pair.length() == 0) {
            return;
        }//  w ww  . j  a v a  2 s . co  m

        int equalAt = pair.indexOf('=');
        String name;
        String value;

        if (equalAt > -1) {
            name = decodeUri(pair.substring(0, equalAt));
            value = decodeUri(pair.substring(equalAt + 1));

        } else {
            name = decodeUri(pair);
            value = null;
        }

        List<String> parameters = map.get(name);

        if (parameters == null) {
            parameters = new ArrayList<String>();
            map.put(name, parameters);
        }

        parameters.add(value);
    }

    /** Decodes the given URI-encoded, UTF-8 {@code string}. */
    public static String decodeUri(String string) {
        if (string == null) {
            return null;
        }
        try {
            return URLDecoder.decode(string, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            throw new IllegalStateException(ex);
        }
    }
}

Related

  1. canonicalize(final SortedMap sortedParamMap)
  2. canonicalize(final SortedMap sortedParamMap)
  3. constructParameterString(TreeMap pPostParms)
  4. convertHttpParameters(Map parameters)