Java URL Build queryStringToMap(String queryString, String charSet)

Here you can find the source of queryStringToMap(String queryString, String charSet)

Description

query String To Map

License

Apache License

Declaration

public static Map queryStringToMap(String queryString, String charSet) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;

import java.util.HashMap;

import java.util.Map;

public class Main {
    public static Map queryStringToMap(String queryString, String charSet) {
        if (queryString == null) {
            throw new IllegalArgumentException("queryString must be specified");
        }//from   w w  w .ja v  a 2 s. c  om

        int index = queryString.indexOf("?");
        if (index > 0) {
            queryString = queryString.substring(index + 1);
        }
        String[] keyValuePairs = queryString.split("&");
        Map<String, String> map = new HashMap<String, String>();
        for (String keyValue : keyValuePairs) {
            if (keyValue.indexOf("=") == -1) {
                continue;
            }
            String[] args = keyValue.split("=");
            if (args.length == 2) {
                try {
                    map.put(args[0], URLDecoder.decode(args[1], charSet));
                } catch (UnsupportedEncodingException e) {
                    throw new IllegalArgumentException("invalid charset : " + charSet);
                }
            }
            if (args.length == 1) {
                map.put(args[0], "");
            }
        }
        return map;
    }
}

Related

  1. queryString(final Map values)
  2. queryStringFromMap(Map map)
  3. queryStringify(HashMap qString)
  4. queryStringToMap(String query, Boolean decode)
  5. queryStringToMap(String queryString)
  6. queryStringToMap(String queryString, String charset)
  7. queryToMap(final String query)
  8. queryToParams(String query)