XMPP Smack 4.1.0 rc1 异常重新加载名册

Posted

技术标签:

【中文标题】XMPP Smack 4.1.0 rc1 异常重新加载名册【英文标题】:XMPP Smack 4.1.0 rc1 Exception reloading roster 【发布时间】:2015-03-23 01:34:21 【问题描述】:

我正在尝试通过一个简单的 android 应用程序连接到我的 XMPP 服务器,我将使用 smack 4.1.0 文档制作该应用程序,即使我遇到了许多错误并且我也是初学者。错误说明如下:

错误:

03-22 23:24:15.566    1447-1460/com.example.xmpp_app I/System.out﹕ ##########################################################
03-22 23:24:20.566    1447-1467/com.example.xmpp_app E/Roster﹕ Exception reloading roster
    org.jivesoftware.smack.SmackException$NoResponseException: No response received within packet reply timeout. Timeout was 5000ms (~5s)
            at org.jivesoftware.smack.AbstractXMPPConnection$6.run(AbstractXMPPConnection.java:1468)

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.xmpp_app" >
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

主活动:

package com.example.xmpp_app;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

import java.io.IOException;

public class MainActivity extends ActionBarActivity 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        connect();

    

    public void connect()
        AsyncTask<Void, Void, Boolean> connectionThread = new AsyncTask<Void, Void, Boolean>()
            @Override
            protected Boolean doInBackground(Void... arg0)
                boolean isConnected = false;

                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);

                // Create the configuration for this new connection
                XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();

                configBuilder.setUsernameAndPassword("testUser", "test123");
                configBuilder.setServiceName("example.com");
                configBuilder.setHost("xx.xx.xx.xx");
                configBuilder.setPort(5222);

                configBuilder.setResource("test");
                configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);

                AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
                // Connect to the server
                try 
                    connection.connect();

                 catch (SmackException e) 

                    e.printStackTrace();
                 catch (IOException e) 

                    e.printStackTrace();
                 catch (XMPPException e) 
                    e.printStackTrace();
                
                // Log into the server
                try 
                    connection.login();
                    System.out.println("##########################################################");
                 catch (XMPPException e) 
                    e.printStackTrace();
                 catch (SmackException e) 
                    e.printStackTrace();
                 catch (IOException e) 
                    e.printStackTrace();
                

                // Disconnect from the server
                connection.disconnect();

                return isConnected;
            
        ;
        connectionThread.execute();
    



    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) 
            return true;
        

        return super.onOptionsItemSelected(item);
    

这个错误是关于什么的,我做错了什么?请帮帮我。

【问题讨论】:

【参考方案1】:

在您的连接通过身份验证后尝试设置名册。如下

@Override
public void authenticated(XMPPConnection arg0, boolean arg1) 

    Log.i(TAG, "authenticated");

    setRoster();


这样设置名册

private void setRoster()

    Log.i(TAG, "setting up roster...");

    roster=Roster.getInstanceFor(connection);

    roster.addRosterListener(this);
    roster.setSubscriptionMode(Roster.SubscriptionMode.manual);

    Log.i(TAG, "***roster set***"); 


打印名册条目

private void printRosterEntries()

    Log.i(TAG, "print roster entries...");

    if (!roster.isLoaded())
        try 
            roster.reloadAndWait();
         catch (NotLoggedInException e) 
            Log.i(TAG, "NotLoggedInException");
            e.printStackTrace();
         catch (NotConnectedException e) 
            Log.i(TAG, "NotConnectedException");
            e.printStackTrace();
         catch (InterruptedException e) 
            Log.i(TAG, "InterruptedException");
            e.printStackTrace();
        

    Collection<RosterEntry> entries=roster.getEntries();

    Log.i(TAG, "entries: "+entries.size());

    for(RosterEntry entry:entries)
    
        Log.i(TAG, "name: "+entry.getName());
        Log.i(TAG, "user: "+entry.getUser());
        Log.i(TAG, "status: "+entry.getStatus());
        Log.i(TAG, "type: "+entry.getType());
    

    Log.i(TAG, "***printing done***");


【讨论】:

【参考方案2】:

经过几个小时的研究,我找到了答案,我在添加 configBuilder.setDebuggerEnabled(true); 后修复了这个问题,请查看以下信息以获取更多信息:aSmack 4.0.* XMPPTCPConnection can't connect to OpenFire and Ejabbered (SmackException$NoResponseException)

.
.
.
configBuilder.setResource("test");
configBuilder.setDebuggerEnabled(true);
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
.
.
.

【讨论】:

以上是关于XMPP Smack 4.1.0 rc1 异常重新加载名册的主要内容,如果未能解决你的问题,请参考以下文章

smack 使用 4.1.0 rc1 在 muc 中接收消息

使用 Smack 4.1.0 的 GCM XMPP 服务器 - 需要添加哪些 jar

XMPP Smack 4.1.0 检查用户是不是已经注册

当使用 Smack 4.1.0 API 作为 Google 的 GCM CCS 的 XMPP 客户端时,SecurityMode.required 不起作用

Android基于XMPP Smack Openfire下学习开发IM连接断开重连

android中的XMPP连接使用smack