Java URL Build queryToParams(String query)

Here you can find the source of queryToParams(String query)

Description

Parse given query string of the form name1=value1&name2=value2 and return it as Map

License

Open Source License

Parameter

Parameter Description
query Query string

Exception

Parameter Description
UnsupportedEncodingException an exception

Return

Parsed results

Declaration

public static Map<String, String> queryToParams(String query) 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 {
    /**//from  www .j  av a  2  s  .  c o m
     * Parse given query string of the form <code>name1=value1&name2=value2</code> and return it as {@link Map}
     * 
     * @param query   Query string
     * @return      Parsed results   
     * @throws UnsupportedEncodingException
     */
    public static Map<String, String> queryToParams(String query) throws UnsupportedEncodingException {
        Map<String, String> query_pairs = new HashMap<String, String>();
        String[] pairs = query.split("&");
        for (String pair : pairs) {
            int idx = pair.indexOf("=");
            query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"),
                    URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
        }
        return query_pairs;
    }
}

Related

  1. queryStringToMap(String query, Boolean decode)
  2. queryStringToMap(String queryString)
  3. queryStringToMap(String queryString, String charset)
  4. queryStringToMap(String queryString, String charSet)
  5. queryToMap(final String query)