If you want to parse query strings on Android? 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.
Below is the simplest code:
1 2 3 4 5 | 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"); // get your value |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.