Because at the end of the day you're handling URI's you can pass them as query parameters. Note that you should base-64 encode the parameters before interpolating the string as it's otherwise unusable.
If you use Android's Uri
class that's handled for you already and can write the following:
Uri
.Builder()
.scheme("https")
.authority("forms.mysitename.in")
.appendPath("solve")
.appendPath("$randomFormId")
.query("title=$title&description=$description&image=$imageUrl")
.build()
Assuming your image parameter is a Url. If it's not a URL you can use the Base64 encoded version into the query parameter but that's not advisable
The following code:
private fun makeUri(): Uri = with(Uri.Builder()) {
val randomFormId = UUID.randomUUID()
val title = "og:meow:My title with spaces and emoji "
val description = "A description :)"
val imageUrl ="https://images.pexels.com/photos/45201/kitty-cat-kitten-pet-45201.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
scheme("https")
authority("forms.mysitename.in")
appendPath("solve")
appendPath("$randomFormId")
appendQueryParameter("title", title)
appendQueryParameter("description", description)
appendQueryParameter("image", imageUrl)
build()
}
Log.d("Test", "${makeUri()}")
Prints:
https://forms.mysitename.in/solve/a6d37c1f-ad7d-46f4-87ef-8e77a9159d6a?title=og%3Ameow%3AMy%20title%20with%20spaces%20and%20emoji%20%F0%9F%91%80&description=A%20description%20%3A)&image=https%3A%2F%2Fimages.pexels.com%2Fphotos%2F45201%2Fkitty-cat-kitten-pet-45201.jpeg%3Fauto%3Dcompress%26cs%3Dtinysrgb%26dpr%3D1%26w%3D500
Which is a valid Uri.
You can also use this following function to create a new Uri from an old one:
private fun fromUri(
uri: Uri,
newTitle: String = uri.getQueryParameter("title") ?: "",
newDescription: String = uri.getQueryParameter("description") ?: "",
newImageUrl: String = uri.getQueryParameter("imageUrl") ?: "",
) = with(Uri.Builder()) {
scheme(uri.scheme)
authority(uri.authority)
uri.pathSegments.forEach { pathSegment -> appendPath(pathSegment) }
// Trick to not add it if it's empty
newTitle.takeIf { it.isNotEmpty() }?.let { appendQueryParameter("title", it)}
newDescription.takeIf { it.isNotEmpty() }?.let { appendQueryParameter("description", it)}
newImageUrl.takeIf { it.isNotEmpty() }?.let { appendQueryParameter("imageUrl", it)}
build()
}