2

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}
2
  • did you try the standard parser provided in java.net? Commented Nov 15, 2022 at 22:13
  • I am using import android.net.Uri Commented Nov 15, 2022 at 22:20

2 Answers 2

3

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]}
Sign up to request clarification or add additional context in comments.

2 Comments

Uri object is part of android.net package but not Kotlin implementation.
So? OP never indicated any external libraries had to be Kotlin implementation. Considering op has the Android tag, added a comment, and added their own answer all indicating using android.net.Uri, pretty sure this is fine. You're welcome to add your own answer that uses pure Kotlin though.
3

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)}")
    }


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.