Android XMPP connection is not persistant - asmack library even running in a separate thread

前端 未结 2 668
闹比i
闹比i 2021-01-15 20:49

I am developing an Android chat app which uses asmack library. What I observe is that the XMPP connection beocmes disconnected after a particular interval of time. ( This al

相关标签:
2条回答
  • 2021-01-15 21:07

    It will be better to use AsyncTask for connection in your code rather than using Thread.It is not recommended to use Thread in Android.Try AsyncTask, it is used for all connection related purposes.

    class ConnectServer extends AsyncTask<Void, Void, XMPPConnection> {
    
    }
    
    0 讨论(0)
  • 2021-01-15 21:17

    After a long struggle, here are my observations and I was able to find a solution to the issue. Any feedback about my solution is welcome

    The Edit holds the answer.

    I simply modified the code from Runnable to a Thread

    When the code which connects to the chat server was in a Runnable, I got a android.os.NetworkOnMainThreadException. For some reason, I am not able to find the stack trace of NetworkOnMainThreadException. The ConnectionConfiguration was carrying the NetworkOnMainThreadException when the connection is made using a Runnable.

    Hope this helps someone who face a similar issue to mine. I sincerely thank @Flow for his effort in helping me resolve this issue. Your smack work is very commendable and a blessing for the android community.

    See the image attached for the exception. enter image description here

    0 讨论(0)
提交回复
热议问题