Java URL Build queryToMap(final String query)

Here you can find the source of queryToMap(final String query)

Description

query To Map

License

Open Source License

Declaration

public static Map<String, String> queryToMap(final String query) 

Method Source Code


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

import com.google.common.base.Strings;

import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;

import java.util.HashMap;

import java.util.Map;

public class Main {
    public static Map<String, String> queryToMap(final String query) {
        final Map<String, String> map = new HashMap<String, String>();
        final String[] keyValuePairs = query.split("&");

        for (String KeyValuePair : keyValuePairs) {
            final String[] KeyValue = KeyValuePair.split("=");

            try {
                map.put(URLDecoder.decode(KeyValue[0], "UTF-8"), Strings.nullToEmpty(KeyValue[1]));
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException("This method requires UTF-8 encoding support", e);
            }// w w w. j a v a  2 s . c om

        }

        return map;
    }
}

Related

  1. queryStringify(HashMap qString)
  2. queryStringToMap(String query, Boolean decode)
  3. queryStringToMap(String queryString)
  4. queryStringToMap(String queryString, String charset)
  5. queryStringToMap(String queryString, String charSet)
  6. queryToParams(String query)