I'm trying to parse query parameters from a URL string using Uri.getQueryParameter(String key) but that method returns null
for the passed-in key. That URL that I'm using is /endpoint/path?query1=foo&page[size]=1
. I'm able to successfully get the value for query1
parameter but not for page[size]
(the method returns null
for this key). My code looks like:
import android.net.Uri
val uri = Uri.parse("/endpoint/path?query1=foo&page[size]=1")
val queryParameterNames = uri.getQueryParameterNames()
println(queryParameterNames) // prints ["query1", "page[size]"]
val map = mutableMapOf<String, String>()
queryParameterNames.forEach { name ->
map[name] = uri.getQueryParameter(name) ?: ""
}
println(map) // prints {query1=foo, page[size]=}
As seen in the output of the last line, the value of page[size]
is empty. I suspect it has something to do with [
, ]
characters in the query parameter name for page[size]
, with them being url encoded while looking for the value but I'm not sure why this is actually failing. So, I have a couple of questions here:
- Why is it successful in finding the names for the query parameters but fails when finding their corresponding values?
- How can I get the value of
page[size]
parameter from the url string in Android?