Home > other >  How can i parse code from URL in Android?
How can i parse code from URL in Android?

Time:10-01

I have next uri:

https://91c2ee0d-05ff-4611-2192-8472eabe98f1.example.org/redirect#code=d5215173-2be3-4491-bf2a-d9d039a50e97&id_token=eyJhbGciOiJQU

How i can parse my code?

d5215173-2be3-4491-bf2a-d9d039a50e97

I tried to do smth like this:

val uri: Uri = Uri.parse(url) 
val code: String = uri.getQueryParameter("code").toString()

But it doesn't work for me.

CodePudding user response:

Right now i solved this problem with next solution:

val url: String = Uri.parse(url).toString()
var code = ""
if (url.contains("#code=")) {
    code = url.substring(url.indexOf("#code=")   6)
    if (code.contains("&")) {
        code = code.split("&")[0]
    }
}

CodePudding user response:

Also you could do it with a Regex:

val url = Uri.parse(url).toString()
var code = ""
val matcher = Pattern.compile("#code=. &").matcher(url)
if (matcher.find())
    code = url.slice(matcher.start()   6 until matcher.end() - 1)
  • Related