HTTP Request in Kotlin

后端 未结 11 2479
不知归路
不知归路 2020-11-28 06:59

I\'m completely new to Kotlin. I want to do a login validation using POST method and to get some information using GET method. I\'ve URL, server Username and Password alread

11条回答
  •  眼角桃花
    2020-11-28 07:30

    Maybe the simplest GET

    For everybody stuck with NetworkOnMainThreadException for the other solutions: use AsyncTask or, even shorter, (yet still experimental) Coroutines:

    launch {
    
        val jsonStr = URL("url").readText()
    
    }
    

    If you need to test with plain http don't forget to add to your manifest: android:usesCleartextTraffic="true"


    For the experimental Coroutines you have to add to build.gradle as of 10/10/2018:

    kotlin {
        experimental {
            coroutines 'enable'
        }
    }
    dependencies {
        implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.24.0"
        implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:0.24.0"
        ...
    

提交回复
热议问题