我正在尝试开发一个聊天应用程序,当互联网稳定时,一切正常。就像我可以发送和接收消息。但是当互联网关闭一段时间后,xmpp会关闭连接。当互联网回来时,我正在尝试重新连接到服务器。成功重新连接后,我收到 Client already logged in
的Stream错误,连接自动关闭。
这是我的广播接收器代码,用于检查互联网连接。
networkReceiver = new BroadcastReceiver (){
@Override
public void onReceive(Context context, Intent intent) {
//super.onReceive(context, intent);
if(intent.getExtras()!=null) {
NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) {
enableChat();
}
}
else if(intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
disableChat();
}
else{
disableChat();
}
}
};
这是启用聊天和禁用聊天的代码。
public static void disableChat() {
msg_edittext.setClickable(false);
sendButton.setClickable(false);
form.setClickable(false);
msg_edittext.setEnabled(false);
sendButton.setEnabled(false);
form.setEnabled(false);
showNoInternetView();
if (xmpp.getConnection()!=null)
xmpp.getConnection().disconnect();
}
public static void showNoInternetView() {
linrNoInternet.setVisibility(View.VISIBLE);
msgListView.setVisibility(View.GONE);
}
public static void showInternetView() {
linrNoInternet.setVisibility(View.GONE);
msgListView.setVisibility(View.VISIBLE);
}
public static void enableChat() {
msg_edittext.setClickable(true);
sendButton.setClickable(true);
form.setClickable(true);
showInternetView();
msg_edittext.setEnabled(true);
sendButton.setEnabled(true);
form.setEnabled(true);
xmpp.connect("reconnect..");
}
尝试重新连接时显示的错误日志是..
07-04 10:25:52.880 8581-8612/com.vario.community D/xmpp: Authenticated!
07-04 10:25:52.880 8581-9732/com.vario.community D/SMACK: SENT (0): <iq id='BpGkK-5' type='get'><query xmlns='jabber:iq:roster'></query></iq>
07-04 10:25:52.882 8581-8612/com.vario.community D/SMACK: XMPPConnection authenticated (0)
07-04 10:25:52.882 8581-8612/com.vario.community I/LOGIN: Yey! We're connected to the Xmpp server!
07-04 10:25:52.883 8581-8612/com.vario.community D/SMACK: XMPPConnection connected (0)
07-04 10:25:52.883 8581-9732/com.vario.community D/SMACK: SENT (0): <presence id='BpGkK-6'><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.igniterealtime.org/projects/smack' ver='E6FpoDPmjMQ3i53V+HWh0YBrS7U='/></presence>
07-04 10:25:52.884 8581-9732/com.vario.community D/SMACK: SENT (0): <iq id='BpGkK-18' type='set'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'></bind></iq>
07-04 10:25:52.885 8581-9732/com.vario.community D/SMACK: SENT (0): <r xmlns='urn:xmpp:sm:3'/>
07-04 10:25:52.886 8581-9732/com.vario.community D/SMACK: SENT (0): <iq id='BpGkK-20' type='get'><query xmlns='jabber:iq:roster'></query></iq>
07-04 10:25:52.886 8581-8612/com.vario.community E/(reconnect..): SMACKException: Client is already logged in
07-04 10:25:52.888 8581-9732/com.vario.community D/SMACK: SENT (0): <presence id='BpGkK-21'><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.igniterealtime.org/projects/smack' ver='JcQq0xyVq6PbVQSN9PSQq58gxiQ='/></presence>
07-04 10:25:52.890 8581-8581/com.vario.community V/TextView: stopSelectionActionMode()
07-04 10:25:52.893 8581-9732/com.vario.community D/SMACK: SENT (0): <presence id='BpGkK-22' type='unavailable'><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.igniterealtime.org/projects/smack' ver='JcQq0xyVq6PbVQSN9PSQq58gxiQ='/></presence>
07-04 10:25:52.893 8581-9732/com.vario.community D/SMACK: SENT (0): <a xmlns='urn:xmpp:sm:3' h='0'/>
07-04 10:25:52.894 8581-9732/com.vario.community D/SMACK: SENT (0): </stream:stream>
07-04 10:25:57.935 8581-9032/com.vario.community E/Roster: Exception reloading roster
org.jivesoftware.smack.SmackException$NoResponseException: No response received within reply timeout. Timeout was 5000ms (~5s). Used filter: IQReplyFilter: iqAndIdFilter (AndFilter: (OrFilter: (IQTypeFilter: type=error, IQTypeFilter: type=result), StanzaIdFilter: id=BpGkK-20)), : fromFilter (OrFilter: (FromMatchesFilter (full): null, FromMatchesFilter (bare): darshan350@vario.fitness, FromMatchesFilter (full): vario.fitness)).
at org.jivesoftware.smack.AbstractXMPPConnection$6.run(AbstractXMPPConnection.java:1443)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
所以,如果有人知道正确重新连接的过程,请帮助我。我已经搜索过这个问题,但我还没有得到解决方案。
谢谢:)
答案 0 :(得分:1)
这是Smack中的已知错误:SMACK-725。
请注意,我不建议在Android上使用ReconnectionManager。相反,我建议为移动环境实施自己的重新连接逻辑,并利用Android提供的信息(例如,侦听CONNECTIVITY_CHANGED意图)。