【发布时间】:2020-09-19 04:58:29
【问题描述】:
大家好,如果我在 emulator chrome 上写“http://10.0.1.42:8080/state”(这是我的 ip)这个工作,我在获取 http 请求时遇到问题,但是如果我想关闭 http 请求应用程序。有人可以帮我吗?
import android.content.Intent
import android.os.Bundle
import android.widget.ImageButton
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import kotlinx.coroutines.runBlocking
import java.util.*
class kmnActivity : AppCompatActivity() {
val EXTRA_TEXT = "com.example.application.example.EXTRA_TEXT"
var uuid = UUID.randomUUID().toString()
var response = "nothing"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_kmn)
val textView = findViewById<TextView>(R.id.textView3)
val kamenButton = findViewById<ImageButton>(R.id.kamen)
val papierButton = findViewById<ImageButton>(R.id.papier)
val nozniceButton = findViewById<ImageButton>(R.id.noznice)
kamenButton.setOnClickListener {
get_state()
textView.setText(response)
// openRollActivity()
}
papierButton.setOnClickListener {
openRollActivity()
}
nozniceButton.setOnClickListener {
openRollActivity()
}
}
fun get_state(){
val client = HttpClient()
runBlocking {
response = client.get<String>("http://10.0.1.42:8080/state") //Here not working
}
client.close()
}
fun openRollActivity() {
val intent = Intent(this@kmnActivity, rollActivity::class.java)
intent.putExtra("id", uuid)
startActivity(intent)
}
}
或者如果有人有更好的方法,他可以说我。感谢您的帮助。
【问题讨论】:
标签: android-studio kotlin kotlin-coroutines ktor