Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 538 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to do url encoding for query parameters in Kotlin

#1
I am new to Kotlin & I am trying to url encode my url which has query parameters.

```
private const val HREF = "date?July 8, 2019"
private const val ENCODED_HREF = print(URLEncoder.encode(HREF, "utf-8"))
private const val URL = "www.example.com/"+"$ENCODED_HREF"
```

Error: Const 'val' has type 'Unit'. Only primitives and Strings are allowed for `private const val ENCODED_HREF`
Reply

#2
Seems like the returning type of the `print` method is *Unit*, so that's why `ENCODED_HREF` has this type. Just take the URLEncoder part out of the method to fix it:

private const val ENCODED_HREF = URLEncoder.encode(HREF, "utf-8")
Reply

#3
`const` expressions in Kotlin must be known at compile time. Also, as @Stanislav points out, print is a `Unit` (i.e., `void` in Java) method, so printing something destroys its value.

Since your constants are computed, the use of `val` (which is a runtime constant) is appropriate. The following compiles.

```
private const val HREF = "date?July 8, 2019"
private val ENCODED_HREF = java.net.URLEncoder.encode(HREF, "utf-8")
private val URL = "www.example.com/"+"$ENCODED_HREF"
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through