Parsing query strings on Android

android, java, parsing, url

Solution

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.

Problem

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!

Original source

Related problems