I have problem with getting HTTP request on kotlin. I have a simple server written python and I want to write a client in kotlin (Android studio). I want to get a request than curl (curl localhost:8080/state). Can someone help me pls?
server:
import enum
from bottle import route, run, template
class Signs(enum.Enum):
SCRISSORS = enum.auto()
STONE = enum.auto()
PAPER = enum.auto()
NOTHING = enum.auto()
board_state = [Signs.NOTHING, Signs.NOTHING]
final_states = {
(Signs.STONE, Signs.SCRISSORS): "Player 1 wins",
(Signs.SCRISSORS, Signs.STONE): "Player 2 wins",
(Signs.PAPER, Signs.STONE): "Player 1 wins",
(Signs.STONE, Signs.PAPER): "Player 2 wins",
(Signs.SCRISSORS, Signs.PAPER): "Player 1 wins",
(Signs.PAPER, Signs.SCRISSORS): "Player 2 wins",
}
@route('/<raw_player>/<raw_sign>')
def play(raw_player, raw_sign):
try:
player = int(raw_player) - 1
except:
return "Invalid player, expecting int"
try:
sign = Signs[raw_sign.upper()]
except:
return "Invalid sign"
if not player in {0, 1}:
return "Invalid player number"
board_state[player] = sign
return "OK\n"
@route('/state')
def state():
if board_state[0] is Signs.NOTHING and board_state[1] is Signs.NOTHING:
return "Nobody played\n"
if board_state[0] is Signs.NOTHING:
return "Waiting for player 1\n"
if board_state[1] is Signs.NOTHING:
return "Waiting for player 2\n"
if board_state[0] is board_state[1]:
return "Draw\n"
return final_states[tuple(board_state)] + "\n"
@route('/reset')
def reset():
board_state[0] = Signs.NOTHING
board_state[1] = Signs.NOTHING
return "OK\n"
run(host='localhost', port=8080)
client:
package com.example.barcang
import android.content.Intent
import android.os.Bundle
import android.widget.ImageButton
import androidx.appcompat.app.AppCompatActivity
import java.util.*
class kmnActivity : AppCompatActivity() {
val EXTRA_TEXT = "com.example.application.example.EXTRA_TEXT"
var uuid = UUID.randomUUID().toString()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_kmn)
val kamenButton = findViewById<ImageButton>(R.id.kamen)
val papierButton = findViewById<ImageButton>(R.id.papier)
val nozniceButton = findViewById<ImageButton>(R.id.noznice)
kamenButton.setOnClickListener {
//here i want get 'curl localhost:8080/1/stone'
openRollActivity()
}
papierButton.setOnClickListener {
//here i want get 'curl localhost:8080/1/paper'
openRollActivity()
}
nozniceButton.setOnClickListener {
//here i want get 'curl localhost:8080/1/scrissors
openRollActivity()
}
}
fun openRollActivity() {
val intent = Intent(this@kmnActivity, rollActivity::class.java)
intent.putExtra("id", uuid)
startActivity(intent)
}
}
if i click on button papierButton(translate paperButton) i want get http request on my server (curl localhost:8080/1/paper)
One way is to use ktor http client .
Include it in your dependencies. ie build.gradle.kts
:
implementation("io.ktor:ktor-client-core:1.3.2")
implementation("io.ktor:ktor-client-android:1.3.2")
Use it in your project:
runBlocking {
val client = HttpClient() {
engine {
// this is an example how you can tweak client options
followRedirects = true
}
}
val response = client.get<String>("http://localhost:8080/1/stone")
}
In the above example I asked for the response as a string, but you could either get an HTTP response, or deserialised object . Refer to ktor's documentation for more examples.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.