Okhttp3 in Kotlin, String.mediaType() does not work
Asked Answered
E

2

8

This is the code that I am trying to do but it doesn't work.

 val JSON = String.format("application/json; charset=utf-8").toMediaType()
  (headers as Map<String, String>).toHeaders()

I did import: import okhttp3.MediaType What do I need to do to make it recognise the toMediaType and toHeaders methods?

Exploiter answered 5/8, 2019 at 11:50 Comment(2)
Why do you have a call to String.format? It seems unnecessaryRoundtree
I don't in my actual code. But trying to find out why it wouldn't recognise .toMediaType, I tried that to see if it helps. Still a bit knew to kotlin, and well, tried both val JSON : String and the other, wasn't aware I need to import the companion, but now I knowExploiter
A
8

The extension functions are in companion objects:

import okhttp3.MediaType.Companion.toMediaType
import okhttp3.Headers.Companion.toHeaders
Antoneantonella answered 5/8, 2019 at 11:56 Comment(0)
D
15

Adding the import to my gradle file to force version 4.7.2 solved it

implementation "com.squareup.okhttp3:okhttp:4.7.2"
Dehydrate answered 22/10, 2020 at 15:17 Comment(0)
A
8

The extension functions are in companion objects:

import okhttp3.MediaType.Companion.toMediaType
import okhttp3.Headers.Companion.toHeaders
Antoneantonella answered 5/8, 2019 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.