【发布时间】:2021-06-18 19:10:01
【问题描述】:
我想在 Android 中使用 Kotlin 实现一些目标:
如果我单击应用程序上的按钮,应用程序会向 TCP 服务器(我用 python 编写)发送一个字。服务器将返回另一个单词,应用程序将显示一条 toast 消息。
这是我到目前为止所做的,我可以弄清楚发送部分,但我无法让它继续监听套接字以接收来自服务器的消息。
我正在尝试使用协程,但是在网上找到所有资源后,这是我能得到的最好的。
另外,我不确定我是否以正确的方式设置 IP 地址。
提前感谢您的帮助!
'''
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val sendBtn = findViewById<Button>(R.id.sendBtn )
val ipBtn = findViewById<Button>(R.id.ipBtn)
val ipInput = findViewById<TextView>(R.id.ipInput)
var ipAddress: String = "192.168.0.101"
// Below is my attempt to keep listening to the socket, if commented, the sending would work.
// My guess is the IO thread is caught in the while loop so the other coroutines cannot use
// IO thread to send to the server.
CoroutineScope(IO).launch{
val socket = Socket(ipAddress, 9999)
var text = ""
while (true) {
text = BufferedReader(InputStreamReader(socket.inputStream)).readLine()
// if text is not null
// Toast.makeText(this@MainActivity, "Set IP", Toast.LENGTH_SHORT).show()
}
}
suspend fun sendMessage(message:String){
val socket = Socket(ipAddress, 9999)
socket.outputStream.write(message.toByteArray())
socket.close()
}
ipBtn.setOnClickListener {
Toast.makeText(this@MainActivity, "Set IP", Toast.LENGTH_SHORT).show()
ipAddress = ipInput.text.toString()
}
sendBtn .setOnClickListener {
CoroutineScope(IO).launch {
Log.d("TAG", "message")
sendMessage("record")
}
}
'''
【问题讨论】:
-
解决方案可能是使用线程。这是一个很好的(Java)示例:Sending and Receiving Data with Sockets in android
-
@paulsm4 感谢您的建议。我了解到,对于互联网通信,建议使用协程(例如从 API 获取数据)。你能解释一下为什么在我的情况下线程比协程更受欢迎吗?
标签: android kotlin tcp socket.io kotlin-coroutines