我正在尝试将用 Kotlin 编写的客户端 Android 应用程序连接到我 PC 上的 localhost Python 服务器,这样我就可以单击一个按钮,打开一个套接字,向服务器发送一条消息,让服务器发回一条消息,然后关闭它联系。
我已经能够找到一些具有类似功能的代码,但它们都是用 Java 编写的,而且我对此很陌生,所以我无法对其进行适当的调整。
我相信这是最接近我正在寻找的东西,它是来自本网站上一个问题的另一个人的代码,但他们无法让它工作并且没有人回应:
class MainActivity : AppCompatActivity() {
var button: Button? = null
var editText: EditText? = null
var textView: TextView? = null
private var clientSocket: Socket? = null
private var socketIn: BufferedReader? = null
private var socketOut: PrintWriter? = null
private val port = 10000
private val ip = "192.168.*.***"
private var myHandler: MyHandler? = null
private var myThread: MyThread? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val policy =
StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
try {
clientSocket = Socket(ip, port)
socketIn =
BufferedReader(InputStreamReader(clientSocket!!.getInputStream()))
socketOut = PrintWriter(clientSocket!!.getOutputStream(), true)
} catch (e: Exception) {
e.printStackTrace()
}
myHandler = MyHandler()
myThread = MyThread()
myThread!!.start()
button = findViewById<View>(R.id.sendMessage_button) as Button
textView = findViewById<View>(R.id.serverResponse_textView) as TextView
button!!.setOnClickListener {
editText = findViewById<View>(R.id.typeMessage_editText) as EditText
val toSend = editText!!.text.toString()
/*socketPython.Datastack.printStack();
textView.setText((String)SocketwithPython.Datastack.Peek(0));
*/
socketOut!!.write(toSend)
}
}
internal inner class MyThread : Thread() {
override fun run() {
while (true) {
try {
println("Trying to read...")
val data = socketIn!!.readLine()
val msg = myHandler!!.obtainMessage()
msg.obj = data
myHandler!!.sendMessage(msg)
println(data)
socketOut!!.print(
"""
Try
""".trimIndent()
)
socketOut!!.flush()
println("Message sent")
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
internal inner class MyHandler : Handler() {
override fun handleMessage(msg: Message) {
textView!!.text = msg.obj.toString()
}
}
}
我将它转换为 Kotlin,并尝试过一点,但我迷路了,而且我的资源有限。原始代码在这里:Python(Server) Android(Client) Socket Programming。放弃这个想法并从头开始做也可能是值得的,我真的不知道。
我也尝试从这篇文章中改编这个:Socket Programming with Python server and Android client:
class MainActivity : AppCompatActivity() {
private var socket: Socket? = null
companion object {
private const val SERVERPORT = 10000
private const val SERVER_IP = "192.168.1.204"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Thread(ClientThread()).start()
}
fun onClick(view: View?) {
try {
socket = Socket(SERVER_IP, SERVERPORT)
val fromClient = findViewById<View>(R.id.typeMessage_editText) as EditText
val clientMessage = fromClient.text.toString()
val toServer = PrintWriter(socket!!.getOutputStream(), true)
toServer.write(clientMessage)
toServer.close()
} catch (e: UnknownHostException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
} catch (e: Exception) {
e.printStackTrace()
}
}
internal inner class ClientThread : Runnable {
override fun run() {
try {
val serverAddr: InetAddress =
InetAddress.getByName(SERVER_IP)
socket = Socket(serverAddr, SERVERPORT)
} catch (e1: UnknownHostException) {
e1.printStackTrace()
} catch (e1: IOException) {
e1.printStackTrace()
}
}
}
}
谢谢。
我添加了一个视图的截图,以防万一: