Java URL Build queryStringToMap(String query, Boolean decode)

Here you can find the source of queryStringToMap(String query, Boolean decode)

Description

query String To Map

License

Open Source License

Declaration

public static Map<String, String> queryStringToMap(String query, Boolean decode)
            throws UnsupportedEncodingException 

Method Source Code

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

import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;

import java.util.HashMap;

import java.util.Map;

public class Main {
    public static Map<String, String> queryStringToMap(String query) throws UnsupportedEncodingException {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        String[] encodedPairs = query.split("&");
        for (String pair : encodedPairs) {
            String[] splitPair = pair.split("=", -1);
            if (splitPair.length != 2) {
                throw new RuntimeException("query string not properly formatted");
            }/*from  w w w. j  a  v a 2s .c  o m*/

            String encodedKey = splitPair[0];
            String encodedValue = splitPair[1];
            hashMap.put(URLDecoder.decode(encodedKey, "utf-8"), URLDecoder.decode(encodedValue, "utf-8"));
        }
        return hashMap;
    }

    public static Map<String, String> queryStringToMap(String query, Boolean decode)
            throws UnsupportedEncodingException {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        String[] encodedPairs = query.split("&");
        for (String pair : encodedPairs) {
            String[] splitPair = pair.split("=", -1);
            if (splitPair.length != 2) {
                throw new RuntimeException("query string not properly formatted");
            }

            String encodedKey = splitPair[0];
            String encodedValue = splitPair[1];
            if (decode) {
                hashMap.put(URLDecoder.decode(encodedKey, "utf-8"), URLDecoder.decode(encodedValue, "utf-8"));
            } else {
                hashMap.put(encodedKey, encodedValue);
            }
        }
        return hashMap;
    }
}

Related

  1. buildUrlWithFilter(String url, String[] filterArrayString)
  2. queryString(final Map> params)
  3. queryString(final Map values)
  4. queryStringFromMap(Map map)
  5. queryStringify(HashMap qString)
  6. queryStringToMap(String queryString)
  7. queryStringToMap(String queryString, String charSet)
  8. queryStringToMap(String queryString, String charset)
  9. queryToMap(final String query)