gpt4 book ai didi

java - Android XMPP 连接不是持久的 - asmack 库甚至在单独的线程中运行

转载 作者:太空宇宙 更新时间:2023-11-04 14:27:42 25 4
gpt4 key购买 nike

我正在开发一个使用asmack库的Android聊天应用程序。我观察到 XMPP 连接在特定时间间隔后断开。 (这也因设备而异)

我按照以下链接中的说明在单独的线程中运行连接代码

Can't establish a new connection with aSmack 4.0.2

但是我得到以下异常

D/Reconnection Manager(23105): scheduleReconnect: calling tryToConnect
I/System.out(23105): default ping interval is :10
W/System.err(23105): org.jivesoftware.smack.SmackException$ConnectionException
W/System.err(23105): at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:436)
W/System.err(23105): at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:811)
W/System.err(23105): at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:396)
W/System.err(23105): at com.connectlinks.service.ChatService.connectToXmppServer(ChatService.java:309)
W/System.err(23105): at com.connectlinks.service.ChatService.access$0(ChatService.java:291)
W/System.err(23105): at com.connectlinks.service.ChatService$3.run(ChatService.java:280)
W/System.err(23105): at android.os.Handler.handleCallback(Handler.java:733)
W/System.err(23105): at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err(23105): at android.os.Looper.loop(Looper.java:136)
W/System.err(23105): at android.app.ActivityThread.main(ActivityThread.java:5586)
W/System.err(23105): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(23105): at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err(23105): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
W/System.err(23105): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
W/System.err(23105): at dalvik.system.NativeStart.main(Native Method)

不知道我哪里出错了。我从许多 SO 问题中了解到,在单独的线程中连接/重新连接是有效的。但对我来说却没有发生......

下面是我正在使用的代码块

在下面,我最初连接到 XMPP 服务器,一切正常

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
.......
startXmppThread();
.......
}

下面的代码块是我启动 XMPP 线程的地方

public void startXmppThread(){
isConnecting = true;
SmackAndroid.init(ChatService.this);
PingManager.setDefaultPingInterval(10);
SmackConfiguration.setDefaultPacketReplyTimeout(20000);
if(xmppThread == null){
xmppThread=new Thread(xmppRunnable,"connection thread");
xmppThread.start();
}
}

XMPP 可运行代码

Runnable xmppRunnable = new Runnable() {

@Override
public void run() {
Log.d(TAG,"starting now thread :"+Thread.currentThread().getId()+" : "+Thread.currentThread().getName());
Looper.prepare();
try{
connectToXmppServer();
handler.post(mRunnableConnectionMonitor);
}catch (Exception e) {
//handler.removeCallbacks(mRunnableConnectionMonitor);
Log.d(TAG, "Trying to Reconnect from the run catch exception");
scheduleReconnect();
e.printStackTrace();

}

xyzz = new Handler(new Handler.Callback() {

@Override
public boolean handleMessage(android.os.Message msg) {
switch (msg.arg1) {
case 1:
Log.d(TAG, "Got Message to connect again");
try {
connectToXmppServer();
handler.post(mRunnableConnectionMonitor);
} catch (Exception e) {
//handler.removeCallbacks(mRunnableConnectionMonitor);
Log.d(TAG,"Trying to reconnect from the handleMessage case 1");
scheduleReconnect();
e.printStackTrace();
}
break;

case 2:
Log.d(TAG, "Got Message to disconnect");
handler.removeCallbacks(mRunnableConnectionMonitor);
try {
connection.disconnect();
Log.d(TAG, "succesfully disconnected");
}catch (Exception e) {
Log.d(TAG, "Exception while disconnecting");
e.printStackTrace();
}
break;
case 3:
Log.d(TAG,"Trying to reconnect from the handleMessage case 3");
scheduleReconnect();
handler.removeCallbacks(mRunnableConnectionMonitor);
break;
default:
break;
}
return false;
}
});
Looper.loop();
}
};

连接XMPP服务器的代码块

private void connectToXmppServer() throws Exception {
if(config ==null)
config = new ConnectionConfiguration(CHAT_SERVER_IP, 5222,host);
config.setSecurityMode(SecurityMode.disabled);
config.setSendPresence(true);
config.setRosterLoadedAtLogin(false);
if(connection == null)
connection = new XMPPTCPConnection(config);


if(mPingManager == null)
mPingManager = PingManager.getInstanceFor(connection);
mPingManager.unregisterPingFailedListener(mPingFailedListener);
mPingManager.registerPingFailedListener(mPingFailedListener);
System.out.println("default ping interval is :"+mPingManager.getPingInterval());
if(!connection.isConnected())
connection.connect();

if(!ConnectlinksApp.m_sharedHelper.getChatRegistration()){
String mobileID = ConnectlinksApp.m_sharedHelper.getMobileID();
Log.d(TAG, "trying to register with : "+mobileID);
AccountManager accountManager=AccountManager.getInstance(connection);
try{
accountManager.createAccount(mobileID, m_strUserPassword);
Log.d(TAG, "account created successfully"+mobileID);
}catch (Exception e) {
Log.d(TAG, "account already exist"+mobileID);
e.printStackTrace();
}
ConnectlinksApp.m_sharedHelper.setChatRegistration(true);
}

if(!connection.isAuthenticated()){
login();
}
}

重新连接XMPP服务器的代码块

protected void scheduleReconnect() {
if (mReconnectHandler == null) mReconnectHandler = new Handler();
mReconnectHandler.removeCallbacks(mReconnectRunnable);
Log.d("Schedule Reconnect","scheduleReconnect: scheduling reconnect in 10 seconds");
mReconnectHandler.postDelayed(mReconnectRunnable, 10000);
}

重新连接的可运行程序

private final Runnable mReconnectRunnable = new Runnable() {
@Override
public void run() {
Log.d("Reconnection Manager","scheduleReconnect: calling tryToConnect");
try{
connectToXmppServer();
handler.post(mRunnableConnectionMonitor);
Log.d(TAG, "This shold be called");
}catch (Exception e) {
// handler.removeCallbacks(mRunnableConnectionMonitor);
e.printStackTrace();
scheduleReconnect();
}
}
};

我尝试了所有可能性,但未能找到解决方案。遇到同样问题的人可以帮助我吗?感谢您花时间阅读这篇长文。

********************编辑************************异常日志为

10-23 19:48:50.866 I/System.out(15248): This is what I am looking for beginning
10-23 19:48:50.876 E/ERROR (15248): ConnectionException
10-23 19:48:50.876 E/ERROR (15248): org.jivesoftware.smack.SmackException$ConnectionException

10-23 19:48:50.876 E/ERROR (15248): at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:436)

10-23 19:48:50.876 E/ERROR (15248): at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:811)

10-23 19:48:50.876 E/ERROR (15248): at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:396)

10-23 19:48:50.876 E/ERROR (15248): at com.connectlinks.service.ChatService.connectToXmppServer(ChatService.java:314)

10-23 19:48:50.876 E/ERROR (15248): at com.connectlinks.service.ChatService.access$0(ChatService.java:293)

10-23 19:48:50.876 E/ERROR (15248): at com.connectlinks.service.ChatService$3.run(ChatService.java:281)

10-23 19:48:50.876 E/ERROR (15248): at android.os.Handler.handleCallback(Handler.java:733)

10-23 19:48:50.876 E/ERROR (15248): at android.os.Handler.dispatchMessage(Handler.java:95)

10-23 19:48:50.876 E/ERROR (15248): at android.os.Looper.loop(Looper.java:136)

10-23 19:48:50.876 E/ERROR (15248): at android.app.ActivityThread.main(ActivityThread.java:5586)

10-23 19:48:50.876 E/ERROR (15248): at java.lang.reflect.Method.invokeNative(Native Method)

10-23 19:48:50.876 E/ERROR (15248): at java.lang.reflect.Method.invoke(Method.java:515)

10-23 19:48:50.876 E/ERROR (15248): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)

10-23 19:48:50.876 E/ERROR (15248): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)

10-23 19:48:50.876 E/ERROR (15248): at dalvik.system.NativeStart.main(Native Method)

10-23 19:48:50.885 E/ERROR (15248): 54.215.XXX.YYY:5222 Exception: null

10-23 19:48:50.885 I/System.out(15248): This is what I am looking for end

********************异常日志结束************************ *****

所以这基本上意味着它无法识别聊天服务器,但它已启动并正在运行。

我需要在 Ejabberd 服务器中进行任何设置吗?很困惑。请。帮助我。

我用来获取异常的代码是

System.out.println("This is what I am looking for beginning");
Log.e("ERROR", "ConnectionException", e);
for (int i = 0; i < e.getFailedAddresses().size(); i++) {
HostAddress element = e.getFailedAddresses().get(i);
Log.e("ERROR", element.getErrorMessage().toString());
}
System.out.println("This is what I am looking for end");

************************编辑************************ *

经过进一步调查,我了解到 connectToXmppServer 方法位于 Runnable 中。

Runnable xmppRunnable = new Runnable() {    
@Override
public void run() {
.....
connectToXmppServer();
.....
}
}

因此,将 Runnable 更改为这样的线程

Thread xmppRunnable = new Thread(){ 

在此之后,好消息是虽然我无法解决问题(即用户在几个小时内离线),但我收到了异常,该异常指出异常是 NetworkOnMainThreadException

I/System.out(10663): NOT CONNECTED

I/System.out(10663): TRYING TO CONNECT

I/System.out(10663): SSmackThis is what I am looking for beginning

I/System.out(10663): SSmackYour localized messagenull

I/System.out(10663): SSmackYour Messagenull

E/ERROR (10663): SConnectionException

E/ERROR (10663): org.jivesoftware.smack.SmackException$ConnectionException

E/ERROR (10663): at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:436)

E/ERROR (10663): at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:811)

E/ERROR (10663): at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:396)

E/ERROR (10663): at com.connectlinks.service.ChatService.connectToXmppServer(ChatService.java:326)

E/ERROR (10663): at com.connectlinks.service.ChatService.access$0(ChatService.java:293)

E/ERROR (10663): at com.connectlinks.service.ChatService$3.run(ChatService.java:281)

E/ERROR (10663): at android.os.Handler.handleCallback(Handler.java:733)

E/ERROR (10663): at android.os.Handler.dispatchMessage(Handler.java:95)

E/ERROR (10663): at android.os.Looper.loop(Looper.java:136)

E/ERROR (10663): at android.app.ActivityThread.main(ActivityThread.java:5586)

E/ERROR (10663): at java.lang.reflect.Method.invokeNative(Native Method)

E/ERROR (10663): at java.lang.reflect.Method.invoke(Method.java:515)

E/ERROR (10663): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)

E/ERROR (10663): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)

E/ERROR (10663): at dalvik.system.NativeStart.main(Native Method)

I/System.out(10663): MYERRORorg.jivesoftware.smack.SmackException$ConnectionException

I/System.out(10663): SThis is what I am looking for middle

E/SSSERROR(10663): android.os.NetworkOnMainThreadException

I/System.out(10663): SSSERROR android.os.NetworkOnMainThreadException

E/SERROR (10663): 54.XXX.XXX.XXX:5222 Exception: null

I/System.out(10663): SERROR 54.XXX.XXX.XXX:5222 Exception: null

I/System.out(10663): SThis is what I am looking for end

不知道为什么我会收到此异常,尽管我在单独的线程中运行它。任何指示对我来说都非常有用。我希望这个问题能让每个面临这个问题的人受益。

最佳答案

经过长时间的斗争,以下是我的观察结果,我找到了问题的解决方案。欢迎对我的解决方案提出任何反馈

编辑给出了答案。

我只是将代码从Runnable修改为Thread

当连接到聊天服务器的代码位于 Runnable 中时,我收到 android.os.NetworkOnMainThreadException。由于某种原因,我无法找到 NetworkOnMainThreadException 的堆栈跟踪。当使用 Runnable 建立连接时,ConnectionConfiguration 携带 NetworkOnMainThreadException。

希望这可以帮助那些面临与我类似问题的人。我真诚地感谢@Flow 为帮助我解决这个问题所做的努力。您的出色工作非常值得赞扬,也是 Android 社​​区的福音。

请参阅所附图片以了解异常(exception)情况。 enter image description here

关于java - Android XMPP 连接不是持久的 - asmack 库甚至在单独的线程中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26497773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com