In my android kotlin code I want to get the value of a query parameter and when the uri string is like below
example.com/sharelink?id=123
then url.getQueryParameter('id')
returns the correct value.
but if the uri is in hashroute format like below
example.com/#/sharelink?id=123
then url.getQueryParameter('id')
returns null.
Here the uri is not in my control so I cannot change this hashroute format.
Could anyone please help me to resolve the query parameter in a proper way rather than using any string matching.
Thanks
One way to do it just replace #
in the URL and then parse it. Other way you can use uri.fragment
to get the part of request and then call getQueryParameter
on it.
val uri= Uri.parse("example.com/#/sharelink?id=123")
val id= Uri.parse(uri.fragment).getQueryParameter("id")
uri.fragment
will return a String
in this case /sharelink?id=123
I don't know if there is a better way to do this.
You can use UrlQuerySanitizer
import android.net.UrlQuerySanitizer
val sanitizer = UrlQuerySanitizer('example.com/#/sharelink?id=123')
val queryValue = sanitizer.getValue('id')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With