Java URL Query Build getQueryParams(String s)

Here you can find the source of getQueryParams(String s)

Description

Get traditional query parameters as a Java map

License

Apache License

Parameter

Parameter Description
s The query string

Exception

Parameter Description
MalformedURLException If the query string is malformed.

Return

A Java map with key-value pairs derived from the query string

Declaration

public static Map<String, String> getQueryParams(String s) throws MalformedURLException 

Method Source Code


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

import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Map;

public class Main {
    /**/*from   w  ww.  j a v  a2 s. co  m*/
     * Get traditional query parameters as a Java map
     * @param s The query string
     * @return A Java map with key-value pairs derived from the query string
     * @throws MalformedURLException If the query string is malformed.
     */
    public static Map<String, String> getQueryParams(String s) throws MalformedURLException {
        Map<String, String> params = new HashMap<String, String>();

        for (String kv : s.split("&")) {
            String[] parts = kv.split("=");
            if (parts.length != 2) {
                throw new MalformedURLException();
            }
            try {
                String k = URLDecoder.decode(parts[0], "UTF-8");
                String v = URLDecoder.decode(parts[1], "UTF-8");
                params.put(k, v);
            } catch (UnsupportedEncodingException ex) {
                throw new MalformedURLException(ex.getMessage());
            }
        }
        return params;
    }
}

Related

  1. getQueryParameters(URL url)
  2. getQueryParameters(URL url)
  3. getQueryParametersFromUrl(String url)
  4. getQueryParams(String httpUrl)
  5. getQueryParams(String query)
  6. getQueryParams(String url)
  7. getQueryString(final Map parameters)
  8. getQueryString(final Map parameters, final boolean encode)
  9. getQueryString(Map parameters)