Home > Mobile >  How to parse or split URL Address in Kotlin?
How to parse or split URL Address in Kotlin?

Time:11-16

If I have this url address

https://googledroids.com/post.html?limit=25&since=1374196005&md5=d8959d12ab687ed5db978cb078f1e&time=0dfdbac117

How could I get (or split) parameters (avoiding hard coding)?

I need separated values:

  • https
  • googledroids.com
  • /post.html
  • parameters and values: {limit=25, time=0dfdbac117, since=1374196005, md5=d8959d12ab687ed5db978cb078f1e}

CodePudding user response:

The following should work, it gives a list of parameter values rather than just one.

val uri = Uri.parse("https://googledroids.com/post.html?limit=25&since=1374196005&md5=d8959d12ab687ed5db978cb078f1e&time=0dfdbac117")
val host = uri.host // googledroids.com
val protocol = uri.scheme // https
val path = uri.path // /post.html
val parameters = uri.queryParameterNames.associateWith { uri.getQueryParameters(it) } // {limit:[25], time:[0dfdbac117], since:[1374196005], md5:[d8959d12ab687ed5db978cb078f1e]}

CodePudding user response:

Using the class:

import android.net.Uri

We can the values protocol, server, path, parameters and we have the option to get a specific parameter value using uri.getQueryParameter():

    val url = "https://googledroids.com/post.html?limit=25&since=1374196005&md5=d8959d12ab687ed5db978cb078f1e&time=0dfdbac117"
    val uri = Uri.parse(url) 
    val protocol = uri.scheme  // https
    val server = uri.authority  // googledroids.com
    val path = uri.path  // /post.html
    val args = uri.queryParameterNames //size: 4 parameters
    val limit = uri.getQueryParameter("limit") // limit: "25"
    println("value of limit: $limit") 

we can get a list of parameters too (Using this question´s answer (java)):

    val url = "https://googledroids.com/post.html?limit=25&since=1374196005&md5=d8959d12ab687ed5db978cb078f1e&time=0dfdbac117"
    val uri = Uri.parse(url) 
    val protocol = uri.scheme  // https
    val server = uri.authority  // googledroids.com
    val path = uri.path  // /post.html
    val args = uri.queryParameterNames //size: 4 parameters

    val query = uri.query
    val data: MutableMap<String, String> = HashMap()
    for (param in query?.split("&")?.toTypedArray()!!) {
        val params = param.split("=").toTypedArray()
        val paramName = URLDecoder.decode(params[0], UTF_8)
        var value = ""
        if (params.size == 2) {
            value = URLDecoder.decode(params[1], UTF_8)
        }
        data[paramName] = value
    }
    println("$data")  //{limit=25, time=0dfdbac117, since=1374196005, md5=d8959d12ab687ed5db978cb078f1e}

this is a better and easy way to get the paramaters and values using uri.queryParameterNames:

   val uri = Uri.parse(url)
    val protocol = uri.scheme
    val server = uri.authority
    val path = uri.path
    val args = uri.queryParameterNames
    val limit = uri.getQueryParameter("limit")

    val query = uri.query

    for (paramName in uri.queryParameterNames) {
        println("parameter => $paramName | value: ${uri.getQueryParameter(paramName)}")
    }


}
  • Related