XMPP Smack 4.1.0 rc1异常重装名单

时间:2015-03-23 01:34:21

标签: android xmpp smack asmack

我试图通过一个简单的Android应用程序连接到我的XMPP服务器,我将使用smack 4.1.0文档制作,即使我已经通过了许多错误,我也是一个初学者。错误说明如下:

ERROR:

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>

mainActivity:

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

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

2 个答案:

答案 0 :(得分:0)

在验证连接后尝试设置名单。如下

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

}

答案 1 :(得分:-1)

经过几个小时的研究后我找到了答案,我在添加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);
.
.
.