0

I have a class and I connect to the websocket in it. It should send messages that i am processing but when validating it shows an error related to threads appears which I do not know how to solve

also in this activity the recycler view

activity:

class ShowTariffsActivity : AppCompatActivity() {

    var tariffList : ArrayList<WebSocketResponse> = arrayListOf()

    var adapter: TarrifsAdapter? = null

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

        val departureId = intent.getIntExtra("id", 0)

        val client = OkHttpClient.Builder()
            .readTimeout(3, TimeUnit.SECONDS)
            .build()
        val request = Request.Builder()
            .url("ws://68.183.30.45/ws/calculation/${departureId}/")
            .build()
        val wsListener = WebSocketList()
        val webSocket = client.newWebSocket(request, wsListener)

        rcViewTarrif.hasFixedSize()
        rcViewTarrif.layoutManager = LinearLayoutManager(this)
        adapter = TarrifsAdapter(tariffList, this)
        rcViewTarrif.adapter = adapter
    }

    private class WebSocketList: WebSocketListener() {
        override fun onMessage(webSocket: WebSocket, text: String) {
            val tarrifs = ShowTariffsActivity()
            val result = Klaxon().parse<WebSocketResponse>(text)

            if (result != null) {
                tarrifs.tariffList.add(result)
            }

        }

        override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
            webSocket.close(NORMAL_CLOSURE_STATUS, null)
        }

        override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
            Log.d("WebSocket", t.message.toString())
        }

        companion object {
            private val NORMAL_CLOSURE_STATUS = 1000
        }
    }
}

error:

2020-12-18 02:49:50.486 13152-13519/? D/WebSocket: Can't create handler inside thread that has not called Looper.prepare()
2020-12-18 18:18:51.692 13912-14078/? D/WebSocket: Can't create handler inside thread that has not called Looper.prepare()
2020-12-18 18:23:21.269 15026-15102/? D/WebSocket: Can't create handler inside thread that has not called Looper.prepare()
2020-12-18 18:37:00.410 15197-15268/? D/WebSocket: Can't create handler inside thread that has not called Looper.prepare()
2020-12-18 18:38:10.729 15361-15438/? D/WebSocket: Can't create handler inside thread that has not called Looper.prepare()
2020-12-18 18:49:23.152 16017-16088/com.example.kenguruexpress D/WebSocket: Can't create handler inside thread that has not called Looper.prepare()

tried to find an alternative and found:

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

        val departureId = intent.getIntExtra("id", 0)

        Handler(Looper.getMainLooper()).post {
            testWSS(departureId)
        }

        rcViewTarrif.hasFixedSize()
        rcViewTarrif.layoutManager = LinearLayoutManager(this)
        adapter = TarrifsAdapter(tariffList, this)
        rcViewTarrif.adapter = adapter
    }

    private fun testWSS(id: Int) {
        val ws = WebSocketFactory().createSocket("ws://68.183.30.45/ws/calculation/$id/", 5000)

        ws.addListener(object : WebSocketAdapter() {
            override fun onTextMessage(websocket: WebSocket?, text: String?) {
                super.onTextMessage(websocket, text)

                val result = Klaxon().parse<WebSocketResponse>(text!!)
                tariffList.add(result!!)
            }

            override fun onTextMessageError(websocket: WebSocket?, cause: WebSocketException?, data: ByteArray?) {
                super.onTextMessageError(websocket, cause, data)
                Log.d("WebSocket", cause?.message.toString())
            }

            override fun onCloseFrame(websocket: WebSocket?, frame: WebSocketFrame?) {
                super.onCloseFrame(websocket, frame)
                Log.v("WSS", "closing socket")
            }
        })
    }

the call also changed, it seems to be correct, but still the tarrifList is empty, that is, I do not receive messages from the web socket, what could be the problem?

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • [This question](https://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare) seems to deal with a similar issue that you have. – Glory to Russia Dec 18 '20 at 13:57
  • 1
    `val tarrifs = ShowTariffsActivity()` is definitely wrong - you should not be instantiating activities yourself. Anyway, this code alone does not explain that log message fully and there are likely some other problems too. Start by logging the complete Throwable stacktrace to see where that problem is. – laalto Dec 18 '20 at 15:52
  • @laalto val tarrifs = ShowTariffsActivity(), how then to replace this line and make everything right? – Konstantin Korolev Dec 18 '20 at 19:00
  • @laalto the error is related to the thread how can I fix it? – Konstantin Korolev Dec 18 '20 at 19:59

0 Answers0