279

Java EE has ServletRequest.getParameterValues().

On non-EE platforms, URL.getQuery() simply returns a string.

What's the normal way to properly parse the query string in a URL when not on Java EE?


It is popular in the answers to try and make your own parser. This is very interesting and exciting micro-coding project, but I cannot say that it is a good idea.

The code snippets below are generally flawed or broken. Breaking them is an interesting exercise for the reader. And to the hackers attacking the websites that use them.

Parsing query strings is a well defined problem but reading the spec and understanding the nuances is non-trivial. It is far better to let some platform library coder do the hard work, and do the fixing, for you!

spongebob
  • 8,370
  • 15
  • 50
  • 83
Will
  • 73,905
  • 40
  • 169
  • 246
  • Could you post a sample URL, what you are getting from `getQuery()`, and what you want to get as output? – Thomas Owens Nov 03 '09 at 13:20
  • 1
    Are you wanting to do this from a servlet or a JSP page? I need some clarification before I answer. – ChadNC Nov 03 '09 at 13:35
  • I'm trying to do this on Android, but all answers on all platforms would be useful answers that might give pointers (also to others who might come across this question) so don't hold back! – Will Nov 03 '09 at 13:37
  • 1
    Do you also need to parse POST parameters? – Thilo Nov 03 '09 at 13:54
  • 2
    Even if you are on J2EE (or on SE with selected EE packages added via OSGi, like me), this question could make sense. In my case, the query strings / url-encoded POST bodies are processed by a part of the system that's intentionally agnostic to stuff like `ServletRequest`. – Hanno Fietz May 19 '11 at 10:22
  • @Will I don't know if you have already solve your question but this lib http://cxf.apache.org/docs/jax-rs-advanced-features.html helps me!Take a look, support FIQL too – rafa.ferreira Jun 21 '11 at 13:35
  • Another scenario where you need this functionality in non-EE code: parsing the return value of PostQuery.execute() in Apache Pivot – sworisbreathing Nov 22 '12 at 00:27

25 Answers25

213

On Android:

import android.net.Uri;

[...]

Uri uri=Uri.parse(url_string);
uri.getQueryParameter("para1");
Benno
  • 5,640
  • 2
  • 26
  • 31
diyism
  • 12,477
  • 5
  • 46
  • 46
  • 20
    note that this uses the Uri class and not the URI class (Uri is part of android.net, while URI is part of java.net) – Marius Jan 05 '12 at 09:38
  • 5
    Also note that prior to Ice Cream Sandwich, this fails to parse + characters in values to a space character. – rpetrich Nov 07 '12 at 20:51
  • @rpetrich actually the docs say that bug is prior to Jelly Bean, including Ice Cream Sandwich. [ref](https://developer.android.com/reference/android/net/Uri.html#getQueryParameter(java.lang.String)) – Big McLargeHuge Jun 18 '19 at 16:30
71

Since Android M things have got more complicated. The answer of android.net.URI.getQueryParameter() has a bug which breaks spaces before JellyBean. Apache URLEncodedUtils.parse() worked, but was deprecated in L, and removed in M.

So the best answer now is UrlQuerySanitizer. This has existed since API level 1 and still exists. It also makes you think about the tricky issues like how do you handle special characters, or repeated values.

The simplest code is

UrlQuerySanitizer.ValueSanitizer sanitizer = UrlQuerySanitizer.getAllButNullLegal();
// remember to decide if you want the first or last parameter with the same name
// If you want the first call setPreferFirstRepeatedParameter(true);
sanitizer.parseUrl(url);
String value = sanitizer.getValue("paramName");

If you are happy with the default parsing behavior you can do:

new UrlQuerySanitizer(url).getValue("paramName")

but you should make sure you understand what the default parsing behavor is, as it might not be what you want.

spongebob
  • 8,370
  • 15
  • 50
  • 83
Nick Fortescue
  • 43,045
  • 26
  • 106
  • 134
64

On Android, the Apache libraries provide a Query parser:

http://developer.android.com/reference/org/apache/http/client/utils/URLEncodedUtils.html and http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html

Michael Brewer-Davis
  • 14,018
  • 5
  • 37
  • 49
Will
  • 73,905
  • 40
  • 169
  • 246
  • Hi Will, I think the Apache class is still in alpha release (4.1) – Muhammad Hewedy Jun 03 '10 at 13:48
  • @Daziplqa I'm not sure I follow? Its been in the android platform from the beginning and it works for me :) – Will Jun 04 '10 at 05:03
  • 9
    This is available in the apache http client library, not only on Android. Btw, the link to apache was changed. Latest is: http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html – Cristian Vrabie Dec 16 '10 at 13:14
  • 9
    Annoyingly `URLEncodedUtils.parse()` returns a `List` that you would then have to loop through to find the value for a specific key. It would be much nicer if it returned a `Map` like in BalusC's answer. – Asaph Apr 27 '11 at 22:15
  • It's good to have a decent, peer-reviewed roll-your-own version next to a good recommendation for a standard library. Sometimes I don't want to pull in another library for that one place where I need to parse urlencoded strings, and sometimes I might even have that library already in my dependency list. That both alternatives are listed as top answers is once again a great testimony to the SO community. – Hanno Fietz May 19 '11 at 10:38
  • 1
    @Hanno Fietz you mean you *trust* these alternatives? I know they are buggy. I know pointing out the bugs I see will only encourage people to adopt 'fixed' versions, rather than themselves look for the bugs I've overlooked. – Will May 19 '11 at 10:57
  • 1
    @Will - well, I would never just *trust* copy-and-paste snippets I got from any website, and no one should. But here, these snippets are rather well reviewed and commented on and thus `are` really helpful, actually. Simply *seeing* some *suggestions* on what might be wrong with the code is already a great help in thinking for myself. And mind you, I didn't mean to say "roll your own is better", but rather that it's great to have good material for an informed decision in my own code. – Hanno Fietz May 23 '11 at 10:55
  • 8
    I imagine parse returns a list so that it maintain positional ordering and more easily allows duplicate entries. – dhaag23 Oct 27 '11 at 18:41
  • 1
    This actually non-recommended way now, because they deprecated apache http classes in API level 22 and will remove in 23 (Android M) – Kazuki Jun 12 '15 at 18:22
  • Now deprecated in API 23 – Gaurav Sharma Oct 19 '15 at 06:19
26
public static Map<String, List<String>> getUrlParameters(String url)
        throws UnsupportedEncodingException {
    Map<String, List<String>> params = new HashMap<String, List<String>>();
    String[] urlParts = url.split("\\?");
    if (urlParts.length > 1) {
        String query = urlParts[1];
        for (String param : query.split("&")) {
            String pair[] = param.split("=", 2);
            String key = URLDecoder.decode(pair[0], "UTF-8");
            String value = "";
            if (pair.length > 1) {
                value = URLDecoder.decode(pair[1], "UTF-8");
            }
            List<String> values = params.get(key);
            if (values == null) {
                values = new ArrayList<String>();
                params.put(key, values);
            }
            values.add(value);
        }
    }
    return params;
}
dfrankow
  • 20,191
  • 41
  • 152
  • 214
  • 1
    JVM Note: I have implemented an equivalent form of this in Scala using Java Collections; here is the github gist: https://gist.github.com/3504765 – Jay Taylor Aug 28 '12 at 22:10
  • 2
    I suggest changing `String pair[] = param.split("=");` into `String pair[] = param.split("=", 2);` to split the key=value pair only on the first occurrence. I believe it is allowed to have unencoded equals signs in the value. – Dennie Jul 25 '13 at 07:57
  • Thanks @Dennie, added. – dfrankow Dec 08 '20 at 18:52
22

If you have jetty (server or client) libs on your classpath you can use the jetty util classes (see javadoc), e.g.:

import org.eclipse.jetty.util.*;
URL url = new URL("www.example.com/index.php?foo=bar&bla=blub");
MultiMap<String> params = new MultiMap<String>();
UrlEncoded.decodeTo(url.getQuery(), params, "UTF-8");

assert params.getString("foo").equals("bar");
assert params.getString("bla").equals("blub");
moritz
  • 5,094
  • 1
  • 26
  • 33
13

If you're using Spring 3.1 or greater (yikes, was hoping that support went back further), you can use the UriComponents and UriComponentsBuilder:

UriComponents components = UriComponentsBuilder.fromUri(uri).build();
List<String> myParam = components.getQueryParams().get("myParam");

components.getQueryParams() returns a MultiValueMap<String, String>

Here's some more documentation.

Nick Spacek
  • 4,717
  • 4
  • 39
  • 42
  • This is something I'm looking for. My question is how do I get the uri? I'm stuck with maintenance of code that I can not change much and we are not using HttpServlet. Instead just using annotations and Spring (@Get, @Produces(mediaType) and @Path("/dataAsJSON/datafield/{datafield})) Just need to know how to get the query string so that I can parse it as is shown in this example. – Nelda.techspiress Mar 11 '16 at 00:03
6

I have methods to achieve this:

1):

public static String getQueryString(String url, String tag) {
    String[] params = url.split("&");
    Map<String, String> map = new HashMap<String, String>();
    for (String param : params) {
        String name = param.split("=")[0];
        String value = param.split("=")[1];
        map.put(name, value);
    }

    Set<String> keys = map.keySet();
    for (String key : keys) {
        if(key.equals(tag)){
         return map.get(key);
        }
        System.out.println("Name=" + key);
        System.out.println("Value=" + map.get(key));
    }
    return "";
}

2) and the easiest way to do this Using Uri class:

public static String getQueryString(String url, String tag) {
    try {
        Uri uri=Uri.parse(url);
        return uri.getQueryParameter(tag);
    }catch(Exception e){
        Log.e(TAG,"getQueryString() " + e.getMessage());
    }
    return "";
}

and this is an example of how to use either of two methods:

String url = "http://www.jorgesys.com/advertisements/publicidadmobile.htm?position=x46&site=reform&awidth=800&aheight=120";      
String tagValue = getQueryString(url,"awidth");

the value of tagValue is 800

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
5

On Android, I tried using @diyism answer but I encountered the space character issue raised by @rpetrich, for example: I fill out a form where username = "us+us" and password = "pw pw" causing a URL string to look like:

http://somewhere?username=us%2Bus&password=pw+pw

However, @diyism code returns "us+us" and "pw+pw", i.e. it doesn't detect the space character. If the URL was rewritten with %20 the space character gets identified:

http://somewhere?username=us%2Bus&password=pw%20pw

This leads to the following fix:

Uri uri = Uri.parse(url_string.replace("+", "%20"));
uri.getQueryParameter("para1");
Stephen Quan
  • 21,481
  • 4
  • 88
  • 75
5

For a servlet or a JSP page you can get querystring key/value pairs by using request.getParameter("paramname")

String name = request.getParameter("name");

There are other ways of doing it but that's the way I do it in all the servlets and jsp pages that I create.

ChadNC
  • 2,528
  • 4
  • 25
  • 39
  • 3
    HttpServletRequest is part of J2EE which he doesn't have. Also using getParamter() is not really parsing. – Mr. Shiny and New 安宇 Nov 03 '09 at 14:48
  • 3
    Please take the time to read the comment in which I asked for clarification of his question. This answer is in response to his answer to that comment in which he stated, "I'm trying to do this on Android, but all answers on all platforms would be useful answers that might give pointers (also to others who might come across this question) so don't hold back!" I answered his question based off of that comment. If you don't have anything useful to add, don't add anything – ChadNC Nov 03 '09 at 14:55
  • 1
    Don't be too upset. "This doesn't answer the question" is useful to add, IMO. – Mr. Shiny and New 安宇 Nov 03 '09 at 15:08
  • 1
    It doesn't matter Android or not, the question is how to parse String containing URL and get URL parameters out of it. What you're porting here is part of Servlet API, where Servlet container parses incoming parameters from HTTP request for you. It's irrelevant, because question is about parsing String that contains URL, not HTTP request, and not inside Servlet container. – mvmn Feb 06 '12 at 12:42
4

Parsing the query string is a bit more complicated than it seems, depending on how forgiving you want to be.

First, the query string is ascii bytes. You read in these bytes one at a time and convert them to characters. If the character is ? or & then it signals the start of a parameter name. If the character is = then it signals the start of a paramter value. If the character is % then it signals the start of an encoded byte. Here is where it gets tricky.

When you read in a % char you have to read the next two bytes and interpret them as hex digits. That means the next two bytes will be 0-9, a-f or A-F. Glue these two hex digits together to get your byte value. But remember, bytes are not characters. You have to know what encoding was used to encode the characters. The character é does not encode the same in UTF-8 as it does in ISO-8859-1. In general it's impossible to know what encoding was used for a given character set. I always use UTF-8 because my web site is configured to always serve everything using UTF-8 but in practice you can't be certain. Some user-agents will tell you the character encoding in the request; you can try to read that if you have a full HTTP request. If you just have a url in isolation, good luck.

Anyway, assuming you are using UTF-8 or some other multi-byte character encoding, now that you've decoded one encoded byte you have to set it aside until you capture the next byte. You need all the encoded bytes that are together because you can't url-decode properly one byte at a time. Set aside all the bytes that are together then decode them all at once to reconstruct your character.

Plus it gets more fun if you want to be lenient and account for user-agents that mangle urls. For example, some webmail clients double-encode things. Or double up the ?&= chars (for example: http://yoursite.com/blah??p1==v1&&p2==v2). If you want to try to gracefully deal with this, you will need to add more logic to your parser.

Mr. Shiny and New 安宇
  • 13,822
  • 6
  • 44
  • 64
  • That does not explain how to parse or retrieve querystring parameter values – ChadNC Nov 03 '09 at 14:58
  • Right, but a bit cumbersome. For that we already have URLDecoder. – BalusC Nov 03 '09 at 15:18
  • 2
    @ChadNC: the third sentence tells you how to parse: read in one byte at a time and convert to chars. The fourth sentence warns you of special chars. Etc. Maybe you didn't read the answer? – Mr. Shiny and New 安宇 Nov 03 '09 at 15:18
  • @BalusC: URLDecoder works but it has some failure modes if you are trying to be more lenient in what kind of URL you accept. – Mr. Shiny and New 安宇 Nov 03 '09 at 15:19
  • 1
    Agree with @Mr.ShinyAndNew parse query param is not easy. I'm supporting FIQL and this become a REAL pain in the ass. E.g: http://yoursite.com/blah??p1==v1&&p2==v2,p2==v3;p2==v4 – rafa.ferreira Jun 20 '11 at 22:01
4

On Android its simple as the code below:

UrlQuerySanitizer sanitzer = new UrlQuerySanitizer(url);
String value = sanitzer.getValue("your_get_parameter");

Also if you don't want to register each expected query key use:

sanitzer.setAllowUnregisteredParamaters(true)

Before calling:

sanitzer.parseUrl(yourUrl)
Kevin Crain
  • 1,905
  • 1
  • 19
  • 28
3

On Android, you can use the Uri.parse static method of the android.net.Uri class to do the heavy lifting. If you're doing anything with URIs and Intents you'll want to use it anyways.

Patrick O'Leary
  • 223
  • 2
  • 6
3

Origanally answered here

On Android, there is Uri class in package android.net . Note that Uri is part of android.net, while URI is part of java.net .

Uri class has many functions to extract query key-value pairs. enter image description here

Following function returns key-value pairs in the form of HashMap.

In Java:

Map<String, String> getQueryKeyValueMap(Uri uri){
    HashMap<String, String> keyValueMap = new HashMap();
    String key;
    String value;

    Set<String> keyNamesList = uri.getQueryParameterNames();
    Iterator iterator = keyNamesList.iterator();

    while (iterator.hasNext()){
        key = (String) iterator.next();
        value = uri.getQueryParameter(key);
        keyValueMap.put(key, value);
    }
    return keyValueMap;
}

In Kotlin:

fun getQueryKeyValueMap(uri: Uri): HashMap<String, String> {
        val keyValueMap = HashMap<String, String>()
        var key: String
        var value: String

        val keyNamesList = uri.queryParameterNames
        val iterator = keyNamesList.iterator()

        while (iterator.hasNext()) {
            key = iterator.next() as String
            value = uri.getQueryParameter(key) as String
            keyValueMap.put(key, value)
        }
        return keyValueMap
    }
Ramakrishna Joshi
  • 1,442
  • 17
  • 22
  • This one should be an accepted answer as it's showing emoji also. In if required, this can also be used with this answer: org.apache.commons.text.StringEscapeUtils.escapeJava & org.apache.commons.text.StringEscapeUtils.unescapeJava – Pratik Saluja Sep 19 '20 at 05:32
3

Just for reference, this is what I've ended up with (based on URLEncodedUtils, and returning a Map).

Features:

  • it accepts the query string part of the url (you can use request.getQueryString())
  • an empty query string will produce an empty Map
  • a parameter without a value (?test) will be mapped to an empty List<String>

Code:

public static Map<String, List<String>> getParameterMapOfLists(String queryString) {
    Map<String, List<String>> mapOfLists = new HashMap<String, List<String>>();
    if (queryString == null || queryString.length() == 0) {
        return mapOfLists;
    }
    List<NameValuePair> list = URLEncodedUtils.parse(URI.create("http://localhost/?" + queryString), "UTF-8");
    for (NameValuePair pair : list) {
        List<String> values = mapOfLists.get(pair.getName());
        if (values == null) {
            values = new ArrayList<String>();
            mapOfLists.put(pair.getName(), values);
        }
        if (pair.getValue() != null) {
            values.add(pair.getValue());
        }
    }

    return mapOfLists;
}

A compatibility helper (values are stored in a String array just as in ServletRequest.getParameterMap()):

public static Map<String, String[]> getParameterMap(String queryString) {
    Map<String, List<String>> mapOfLists = getParameterMapOfLists(queryString);

    Map<String, String[]> mapOfArrays = new HashMap<String, String[]>();
    for (String key : mapOfLists.keySet()) {
        mapOfArrays.put(key, mapOfLists.get(key).toArray(new String[] {}));
    }

    return mapOfArrays;
}
Dan
  • 2,157
  • 21
  • 15
3

This works for me.. I'm not sure why every one was after a Map, List> All I needed was a simple name value Map.

To keep things simple I used the build in URI.getQuery();

public static Map<String, String> getUrlParameters(URI uri)
    throws UnsupportedEncodingException {
    Map<String, String> params = new HashMap<String, String>();
    for (String param : uri.getQuery().split("&")) {
        String pair[] = param.split("=");
        String key = URLDecoder.decode(pair[0], "UTF-8");
        String value = "";
        if (pair.length > 1) {
            value = URLDecoder.decode(pair[1], "UTF-8");
        }
        params.put(new String(key), new String(value));
    }
    return params;
}
Dave
  • 307
  • 3
  • 2
  • 1
    how about forms with multiple selection? Its perfectly normal to have keys repeated in legitimate query strings (and POST form bodies). There are other defects and corner-cases not covered; many of them have been mentioned in commentary on other approaches. I'll refrain form pointing them out in fear that you fix it instead of using a quality library, as per my *rant* in the question ;) – Will Feb 12 '12 at 15:16
2

Guava's Multimap is better suited for this. Here is a short clean version:

Multimap<String, String> getUrlParameters(String url) {
        try {
            Multimap<String, String> ret = ArrayListMultimap.create();
            for (NameValuePair param : URLEncodedUtils.parse(new URI(url), "UTF-8")) {
                ret.put(param.getName(), param.getValue());
            }
            return ret;
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
pathikrit
  • 32,469
  • 37
  • 142
  • 221
1
if (queryString != null)
{
    final String[] arrParameters = queryString.split("&");
    for (final String tempParameterString : arrParameters)
    {
        final String[] arrTempParameter = tempParameterString.split("=");
        if (arrTempParameter.length >= 2)
        {
            final String parameterKey = arrTempParameter[0];
            final String parameterValue = arrTempParameter[1];
            //do something with the parameters
        }
    }
}
spongebob
  • 8,370
  • 15
  • 50
  • 83
Andreas
  • 2,045
  • 5
  • 19
  • 30
1

Apache AXIS2 has a self-contained implementation of QueryStringParser.java. If you are not using Axis2, just download the sourcecode and test case from here -

http://svn.apache.org/repos/asf/axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/transport/http/util/QueryStringParser.java

http://svn.apache.org/repos/asf/axis/axis2/java/core/trunk/modules/kernel/test/org/apache/axis2/transport/http/util/QueryStringParserTest.java

Sripathi Krishnan
  • 30,948
  • 4
  • 76
  • 83
0

Use Apache HttpComponents and wire it up with some collection code to access params by value: http://www.joelgerard.com/2012/09/14/parsing-query-strings-in-java-and-accessing-values-by-key/

Joel
  • 449
  • 1
  • 5
  • 12
0

using Guava:

Multimap<String,String> parseQueryString(String queryString, String encoding) {
    LinkedListMultimap<String, String> result = LinkedListMultimap.create();

    for(String entry : Splitter.on("&").omitEmptyStrings().split(queryString)) {
        String pair [] = entry.split("=", 2);
        try {
            result.put(URLDecoder.decode(pair[0], encoding), pair.length == 2 ? URLDecoder.decode(pair[1], encoding) : null);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    return result;
}
Tyson
  • 968
  • 1
  • 6
  • 15
0

I don't think there is one in JRE. You can find similar functions in other packages like Apache HttpClient. If you don't use any other packages, you just have to write your own. It's not that hard. Here is what I use,

public class QueryString {

 private Map<String, List<String>> parameters;

 public QueryString(String qs) {
  parameters = new TreeMap<String, List<String>>();

  // Parse query string
     String pairs[] = qs.split("&");
     for (String pair : pairs) {
            String name;
            String value;
            int pos = pair.indexOf('=');
            // for "n=", the value is "", for "n", the value is null
         if (pos == -1) {
          name = pair;
          value = null;
         } else {
       try {
        name = URLDecoder.decode(pair.substring(0, pos), "UTF-8");
              value = URLDecoder.decode(pair.substring(pos+1, pair.length()), "UTF-8");            
       } catch (UnsupportedEncodingException e) {
        // Not really possible, throw unchecked
           throw new IllegalStateException("No UTF-8");
       }
         }
         List<String> list = parameters.get(name);
         if (list == null) {
          list = new ArrayList<String>();
          parameters.put(name, list);
         }
         list.add(value);
     }
 }

 public String getParameter(String name) {        
  List<String> values = parameters.get(name);
  if (values == null)
   return null;

  if (values.size() == 0)
   return "";

  return values.get(0);
 }

 public String[] getParameterValues(String name) {        
  List<String> values = parameters.get(name);
  if (values == null)
   return null;

  return (String[])values.toArray(new String[values.size()]);
 }

 public Enumeration<String> getParameterNames() {  
  return Collections.enumeration(parameters.keySet()); 
 }

 public Map<String, String[]> getParameterMap() {
  Map<String, String[]> map = new TreeMap<String, String[]>();
  for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
   List<String> list = entry.getValue();
   String[] values;
   if (list == null)
    values = null;
   else
    values = (String[]) list.toArray(new String[list.size()]);
   map.put(entry.getKey(), values);
  }
  return map;
 } 
}
ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
0
public static Map <String, String> parseQueryString (final URL url)
        throws UnsupportedEncodingException
{
    final Map <String, String> qps = new TreeMap <String, String> ();
    final StringTokenizer pairs = new StringTokenizer (url.getQuery (), "&");
    while (pairs.hasMoreTokens ())
    {
        final String pair = pairs.nextToken ();
        final StringTokenizer parts = new StringTokenizer (pair, "=");
        final String name = URLDecoder.decode (parts.nextToken (), "ISO-8859-1");
        final String value = URLDecoder.decode (parts.nextToken (), "ISO-8859-1");
        qps.put (name, value);
    }
    return qps;
}
wafna
  • 54
  • 1
0

Answering here because this is a popular thread. This is a clean solution in Kotlin that uses the recommended UrlQuerySanitizer api. See the official documentation. I have added a string builder to concatenate and display the params.

    var myURL: String? = null
    // if the url is sent from a different activity where you set it to a value
    if (intent.hasExtra("my_value")) {
        myURL = intent.extras.getString("my_value")
    } else {
        myURL = intent.dataString
    }

    val sanitizer = UrlQuerySanitizer(myURL)
    // We don't want to manually define every expected query *key*, so we set this to true
    sanitizer.allowUnregisteredParamaters = true
    val parameterNamesToValues: List<UrlQuerySanitizer.ParameterValuePair> = sanitizer.parameterList
    val parameterIterator: Iterator<UrlQuerySanitizer.ParameterValuePair> = parameterNamesToValues.iterator()

    // Helper simply so we can display all values on screen
    val stringBuilder = StringBuilder()

    while (parameterIterator.hasNext()) {
        val parameterValuePair: UrlQuerySanitizer.ParameterValuePair = parameterIterator.next()
        val parameterName: String = parameterValuePair.mParameter
        val parameterValue: String = parameterValuePair.mValue

        // Append string to display all key value pairs
        stringBuilder.append("Key: $parameterName\nValue: $parameterValue\n\n")
    }

    // Set a textView's text to display the string
    val paramListString = stringBuilder.toString()
    val textView: TextView = findViewById(R.id.activity_title) as TextView
    textView.text = "Paramlist is \n\n$paramListString"

    // to check if the url has specific keys
    if (sanitizer.hasParameter("type")) {
        val type = sanitizer.getValue("type")
        println("sanitizer has type param $type")
    }
jungledev
  • 4,195
  • 1
  • 37
  • 52
-2

this method takes the uri and return map of par name and par value

  public static Map<String, String> getQueryMap(String uri) {

    String queryParms[] = uri.split("\\?");

    Map<String, String> map = new HashMap<>();// 

    if (queryParms == null || queryParms.length == 0) return map;

    String[] params = queryParms[1].split("&");
    for (String param : params) {
        String name = param.split("=")[0];
        String value = param.split("=")[1];
        map.put(name, value);
    }
    return map;
}
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
  • 1
    As per my rant above, this can be trivially made to crash. Don't bother fixing, just use a professional utility library instead. – Will Apr 15 '15 at 06:44
-4

You say "Java" but "not Java EE". Do you mean you are using JSP and/or servlets but not a full Java EE stack? If that's the case, then you should still have request.getParameter() available to you.

If you mean you are writing Java but you are not writing JSPs nor servlets, or that you're just using Java as your reference point but you're on some other platform that doesn't have built-in parameter parsing ... Wow, that just sounds like an unlikely question, but if so, the principle would be:

xparm=0
word=""
loop
  get next char
  if no char
    exit loop
  if char=='='
    param_name[xparm]=word
    word=""
  else if char=='&'
    param_value[xparm]=word
    word=""
    xparm=xparm+1
  else if char=='%'
    read next two chars
    word=word+interpret the chars as hex digits to make a byte
  else
    word=word+char

(I could write Java code but that would be pointless, because if you have Java available, you can just use request.getParameters.)

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Jay
  • 26,876
  • 10
  • 61
  • 112