import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import java.io.IOException
import java.lang.Exception
...
private val client = OkHttpClient()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val tvDisplay: TextView = findViewById(R.id.displayTV) as TextView
tvDisplay.setOnClickListener {
tvDisplay.text = run("https://jsonplaceholder.typicode.com/todos/1")
}
}
@Throws(IOException::class)
fun run(url: String): String {
val request = Request.Builder()
.url(url)
.build()
try {
client.newCall(request).execute().use { response -> return response.body().toString() }
}
catch (e: Exception) {
return e.message.toString()
}
}
Using android studio and kotlin. Trying to call an API but all I get is NULL instead of the string it should be getting.
Additionally how do I add basic authentication to this (username/password) if the API required it?
Also what does "@Throws" do?