vault backup: 2024-05-21 09:44:49

This commit is contained in:
2024-05-21 09:44:49 +08:00
parent 2be9e4110d
commit 9b2d33b9d4
507 changed files with 4 additions and 3 deletions

View File

@@ -0,0 +1,55 @@
Ktor是由Kotlin提供的一個framwork。
要在Android使用Ktor需要在build.gradle加入以下的dependency:
```
implementation "io.ktor:ktor:1.2.5"
implementation "io.ktor:ktor-server-netty:1.2.5"
implementation "io.ktor:ktor-gson:1.2.5"
```
`packagingOptions`裡,也需要加入以下的設定來必面編譯問題:
```
packagingOptions {
exclude 'META-INF/*'
}
```
`AndroidManifest.xml`記得加入internet的權限
```
<uses-permission android:name="android.permission.INTERNET"/>
```
然後就是HTTP Server的code了
```
embeddedServer(Netty, 8080) {
install(ContentNegotiation) {
gson {}
}
routing {
get("/") {
call.respond(mapOf("message" to "Hello world"))
}
}
}.start(wait=true)
```
但是這段code會block所以需要一個thread把它包起來
```kotlin
Thread {
httpEngine = embeddedServer(Netty, 8080) {
install(ContentNegotiation) {
gson {}
}
routing {
get("/") {
call.respond(mapOf("message" to "Hello world"))
}
get("/say/{something}") {
call.respond(mapOf("message" to "You say: " + call.parameters["something"]))
activity?.findViewById<TextView>(R.id.textView)?.text = "You say: " + call.parameters["something"]
}
}
}.start(wait = false)
}.start()
```