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?