Once you have set your Android Studio to use Kotlin is pretty simple to do a REST call, and it's pretty much the same logic as with Java.
Here's an example of a REST call with OkHttp:
build.gradle
dependencies {
//...
implementation 'com.squareup.okhttp3:okhttp:3.8.1'
}
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
MainActivity.kt
class MainActivity : AppCompatActivity() {
private val client = OkHttpClient()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
run("https://api.github.com/users/Evin1-/repos")
}
fun run(url: String) {
val request = Request.Builder()
.url(url)
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {}
override fun onResponse(call: Call, response: Response) = println(response.body()?.string())
})
}
}
Below are a few more complicated examples with other libraries:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…