To be exact, I need a Java equivalent for C# HttpUtility.ParseQueryString Method. Please, give me an advice on this. Thanks.
If you are looking for a way to achieve it without using an external library, the following code will help you.
You can access the returned Map using <map>.get("client_id"), with the URL given in your question this would return "SS".
UPDATE As this answer is still quite popular, I made an improved version of the method above, which handles multiple parameters with the same key and parameters with no value as well.
UPDATE: UriComponentsBuilder comes from Spring. Here the link.
If you're using Java 8 and you're willing to write a few reusable methods, you can do it in one line.
But that's a pretty brutal line.
For Android, if you are using OkHttp in your project. You might get a look at this. It simple and helpful.
This solution on the other hand collects a map (given that in a url there can be more parameters with same name but different values).
Both the solutions must use an utility function to properly decode the parameters.
Returns a java.util.Map of the parameters of this request.
Returns: an immutable java.util.Map containing parameter names as keys and parameter values as map values. The keys in the parameter map are of type String. The values in the parameter map are of type String array.
Using above mentioned comments and solutions, I am storing all the query parameters using Map<String, Object> where Objects either can be string or Set<String>. The solution is given below. It is recommended to use some kind of url validator to validate the url first and then call convertQueryStringToMap method.
I had a go at a Kotlin version seeing how this is the top result in Google.
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.
textView.text = "Paramlist is \n\n$paramListString"
If you are using Spring, add an argument of type @RequestParam Map<String,String> to your controller method, and Spring will construct the map for you!
Not the answer you're looking for? Browse other questions tagged java parsing uri namevaluecollection or ask your own question.
How could i extract values from GET hyperlink in java?
How do i write algorithm for Map and Regex together to check inputs?
load a string in a map using guava splitter?
How to convert map to url query string?