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

标签 android xmpp smack asmack

我正在尝试通过一个简单的 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)

list :

<?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>

主要 Activity :

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);
    }
}

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

最佳答案

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

@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***");

}

关于android - XMPP Smack 4.1.0 rc1 异常重新加载花名册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29202043/

相关文章:

android - 如何在 Android Studio 中永久启用外部构建?

android - 是否可以重新创建 Activity 并保留运行时实例?

android - Espresso - 如何在没有断言/ View 交互的情况下等待空闲?

xmpp - Asterisk 中使用 Tigase 和 res_xmpp 的 Pubsub

java - Smack 的 XMPP over TCP 连接在 main() 方法返回后停止

java - 如何在 Java 桌面应用程序中模拟 Android 类型的文件系统?

android - XMPP - 资源绑定(bind)有用性

ios - XMPP 客户端网络故障和 Openfire 离线消息

java - 创建用户,变得 Not Acceptable (406)

java - org.jivesoftware :smack:jar:3. 4.1-0cec571 的 POM 丢失