0

I have an app that communicates with a server during sockets. first I did this:

val sock = Socket("192.168.1.108", 5000)

and the app crashed because of the error: "android.os.NetworkOnMainThreadException", I read about it and I found a solution for that error and the solution is to create a syncTask as an inner class, and this is what I did:

class randomChat : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.chat_show)

        HandleServer().execute()
    }

    inner class HandleServer: AsyncTask<String, String, String>() {

        override fun doInBackground(vararg p0: String?): String {
            val sock = Socket("192.168.1.108", 5000)
            sock.getInputStream()

            sock.use {
                it.outputStream.write("hello socket world".toByteArray())
            }
            return "Good"
        }

    }
}

and that fixed the error, But did not fix my needs... basically, my needs are to have a conversion between the server and the user who use the app, the user will have an editText view and a button to send the data to the server and a textView that always change based on server data.

So: What I need to have is:

  1. open socket that receives data all the time from the server and updates a view in the activity
  2. I need to be able to have an editText in the activity that by user click it's send data to the server (with the socket)

Thank you very much !!!!!

Njeru Cyrus
  • 1,753
  • 21
  • 22
Adam
  • 299
  • 1
  • 4
  • 19
  • onPostExecute() method of AsyncTask is invoked in UI thread. You can get data inside that method with correct parameters and change properties of UI elements such as text view. Please, Google AsyncTask for more info. – Thracian Jun 04 '18 at 19:12
  • @Thracian Thanks, this solve my first problem, can you please help me with the other problem? (this is the major one..). – Adam Jun 04 '18 at 19:18
  • Look for "java socket chat" examples, there are plenty. You are going to have to learn about threads. – ESala Jun 04 '18 at 22:15

2 Answers2

0

You can implement on ongoing socket like this, and just call the sendDataToNetwork() method for the editText

Example: Android bi-directional network socket using AsyncTask

There are better ways to manage threads or use libraries like RxJava, but for a basic simple implementation the above should work.

jt-gilkeson
  • 2,661
  • 1
  • 30
  • 40
0

Instead of bothering with the AsyncTask, I highly recommend you to use coroutines instead:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.chat_show)
    launch (CommonPool) {
        val sock = Socket("192.168.1.108", 5000)
        sock.use {
            it.outputStream.write("hello socket world".toByteArray())
            withContext(UI) {
                // update the view
            }
            // more socket ops
            withContext(UI) {
                // update the view again
            }
        }
    }
}
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436