I will say right away that I started to study Kotlin recently, and I just want to rewrite one program, and therefore I took up this
I want to read the text received from the incoming stream, but as a result, I get the following
Perhaps I make terribly stupid mistakes, but I hardly understand what and how it works here (I read half Internet)
PS I already tried to use this, but the program just doesn't go further and that's it
package com.example.appisone
import java.net.Socket
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import kotlinx.coroutines.*
import java.io.InputStreamReader
import java.util.*
class MainActivity : AppCompatActivity() {
private var Text_Edit: EditText? = null
private var btn: Button? = null
private var Text_view: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Text_Edit = findViewById(R.id.EditText)
btn = findViewById(R.id.Button)
Text_view = findViewById(R.id.TextView)
btn?.setOnClickListener {
Text_view?.text = Text_Edit?.text
println("Button is pressed!")
GlobalScope.launch(Dispatchers.IO) {
send_text()
}
}
}
private fun send_text() {
var data: Any
println("send_text is pressed!")
val client = Socket("192.168.0.3", 9090)
val text = Text_Edit?.text.toString()
println(text.toByteArray())
client.getOutputStream().write(text.toByteArray())
val reader = client.getInputStream()
data = reader.toString()
println("Сообщение --- >$data")
}
} ```
Thanks in advance for your wasted time
You don't read from a Reader
like this:
reader.toString()
That just renders the Reader
>>object<< as a string. Since toString
has not been overridden by Reader
, you get the Object::toString
implementation. (It prints the internal class name and the object's "identity hash code" in hexadecimal.)
To read from a Reader
use one of the read
methods in the Reader API , or wrap it in a Scanner
or a BufferedReader
so that you can use those APIs.
In your "PS" you are also calling toString() on an object that doesn't override toString(). Based on the class name, it looks like it is a byte[].
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.