Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin uri.getQueryParameter not working with hashroute

Tags:

android

kotlin

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

like image 260
thinuwan Avatar asked Sep 21 '25 09:09

thinuwan


2 Answers

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.

like image 178
ADM Avatar answered Sep 23 '25 08:09

ADM


You can use UrlQuerySanitizer

import android.net.UrlQuerySanitizer

val sanitizer = UrlQuerySanitizer('example.com/#/sharelink?id=123')
val queryValue = sanitizer.getValue('id')
like image 29
thinuwan Avatar answered Sep 23 '25 07:09

thinuwan