如何在安卓上使用 Smack

Posted

技术标签:

【中文标题】如何在安卓上使用 Smack【英文标题】:How to use Smack with android 【发布时间】:2015-09-01 13:00:47 【问题描述】:

我有以下代码使用 smack 4.1.0 与 xmpp 服务器 im.koderoot.net 连接,此主机/服务适用于我使用 smack3.1.0 和 smacx3.1.0 的 java 应用程序,但在我使用 java 时我正在使用它抛出以下异常的罐子

09-01 18:29:18.921: E/SMACK EXCEPTION(1577): org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: im.koderoot.net:5222 Exception: null

MainActivity.java

package com.example.zzxmpp;


import java.io.IOException;

import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;

import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener

    private XMPPTCPConnection mConnection;
    private Button connect;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        connect = (Button) findViewById(R.id.button1);
        connect.setOnClickListener(this);
    

    @Override
    public void onClick(View v) 
        // TODO Auto-generated method stub
          XMPPTCPConnectionConfiguration.XMPPTCPConnectionConfigurationBuilder builder = XMPPTCPConnectionConfiguration.builder();
            builder.setServiceName("im.koderoot.net");
            builder.setPort(5222);
            builder.setResource("SmackAndroidTestClient");
            builder.setUsernameAndPassword("xxxx", "xxxx");
            builder.setRosterLoadedAtLogin(true);


            mConnection = new XMPPTCPConnection(builder.build());
            //Set ConnectionListener here to catch initial connect();
          //  mConnection.addConnectionListener(this);

            try 
                mConnection.connect();
                mConnection.login();
                Toast.makeText(getApplicationContext(), "Sucess", Toast.LENGTH_LONG).show();
             catch (SmackException e) 
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Smack Exception"+e, Toast.LENGTH_LONG).show();
             catch (IOException e) 
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "IO Exception"+e, Toast.LENGTH_LONG).show();
             catch (XMPPException e) 
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "XMPP Exception"+e, Toast.LENGTH_LONG).show();
            

    



我在项目中有以下 .jar 文件

smack-android-4.1.0-alpha6.jar

smack-tcp-4.1.0-alpha6.jar

minidns-0.1.1.jar

smack-sasl-provided-4.1.0-alpha6.jar

smack-core-4.1.0-alpha6.jar

smack-resolver-minidns-4.1.0-alpha6.jar

jxmpp-util-cache-0.4.0-alpha2.jar

jxmpp-core-0.4.0.jar

我自己找到了以下可行的解决方案

用 asmack-2010.05.07.jar 和 asmack-jse-buddycloud-2010.12.11.jar 替换所有 jar 文件

并将代码更改为以下

package com.demo.xmppchat;

import java.util.ArrayList;
import java.util.Collection;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class XMPPChatDemoActivity extends Activity 

    public static final String HOST = "im.koderoot.net";
    public static final int PORT = 5222;
    //public static final String SERVICE = "slogin.oscar.aol.com";

    public static final String USERNAME = "XXXXXXX";
    public static final String PASSWORD = "XXXXXX";

    private XMPPConnection connection;
    private ArrayList<String> messages = new ArrayList<String>();
    private Handler mHandler = new Handler();

    private EditText recipient;
    private EditText textMessage;
    private ListView listview;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        recipient = (EditText) this.findViewById(R.id.toET);
        textMessage = (EditText) this.findViewById(R.id.chatET);
        listview = (ListView) this.findViewById(R.id.listMessages);
        setListAdapter();

        // Set a listener to send a chat text message
        Button send = (Button) this.findViewById(R.id.sendBtn);
        send.setOnClickListener(new View.OnClickListener() 
            public void onClick(View view) 
                String to = recipient.getText().toString();
                String text = textMessage.getText().toString();

                Log.i("XMPPChatDemoActivity", "Sending text " + text + " to " + to);
                Message msg = new Message(to, Message.Type.chat);
                msg.setBody(text);              
                if (connection != null) 
                    connection.sendPacket(msg);
                    messages.add(connection.getUser() + ":");
                    messages.add(text);
                    setListAdapter();
                
            
        );

        connect();
    

    /**
     * Called by Settings dialog when a connection is established with the XMPP
     * server
     * 
     * @param connection
     */
    public void setConnection(XMPPConnection connection) 
        this.connection = connection;
        if (connection != null) 
            // Add a packet listener to get messages sent to us
            PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
            connection.addPacketListener(new PacketListener() 
                @Override
                public void processPacket(Packet packet) 
                    Message message = (Message) packet;
                    if (message.getBody() != null) 
                        String fromName = StringUtils.parseBareAddress(message
                                .getFrom());
                        Log.i("XMPPChatDemoActivity", "Text Recieved " + message.getBody()
                                + " from " + fromName );
                        messages.add(fromName + ":");
                        messages.add(message.getBody());
                        // Add the incoming message to the list view
                        mHandler.post(new Runnable() 
                            public void run() 
                                setListAdapter();
                            
                        );
                    
                
            , filter);
        
    

    private void setListAdapter() 
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.listitem, messages);
        listview.setAdapter(adapter);
    

    @Override
    protected void onDestroy() 
        super.onDestroy();
        try 
            if (connection != null)
                connection.disconnect();
         catch (Exception e) 

        
    

    public void connect() 

        final ProgressDialog dialog = ProgressDialog.show(this,
                "Connecting...", "Please wait...", false);

        Thread t = new Thread(new Runnable() 

            @Override
            public void run() 
                // Create a connection
                ConnectionConfiguration connConfig = new ConnectionConfiguration(
                        HOST, PORT);//, SERVICE);
                XMPPConnection connection = new XMPPConnection(connConfig);

                try 
                    connection.connect();
                    Log.i("XMPPChatDemoActivity",
                            "Connected to " + connection.getHost());
                 catch (XMPPException ex) 
                    Log.e("XMPPChatDemoActivity", "Failed to connect to "
                            + connection.getHost());
                    Log.e("XMPPChatDemoActivity", ex.toString());
                    setConnection(null);
                
                try 
                    SASLAuthentication.supportSASLMechanism("PLAIN", 0);
                    connection.login(USERNAME, PASSWORD);
                    Log.i("XMPPChatDemoActivity",
                            "Logged in as " + connection.getUser());

                    // Set the status to available
                    Presence presence = new Presence(Presence.Type.available);
                    connection.sendPacket(presence);
                    setConnection(connection);

                    Roster roster = connection.getRoster();
                    Collection<RosterEntry> entries = roster.getEntries();
                    for (RosterEntry entry : entries) 
                        Log.d("XMPPChatDemoActivity",
                                "--------------------------------------");
                        Log.d("XMPPChatDemoActivity", "RosterEntry " + entry);
                        Log.d("XMPPChatDemoActivity",
                                "User: " + entry.getUser());
                        Log.d("XMPPChatDemoActivity",
                                "Name: " + entry.getName());
                        Log.d("XMPPChatDemoActivity",
                                "Status: " + entry.getStatus());
                        Log.d("XMPPChatDemoActivity",
                                "Type: " + entry.getType());
                        Presence entryPresence = roster.getPresence(entry
                                .getUser());


                        Log.d("XMPPChatDemoActivity", "Presence Status: "
                                + entryPresence.getStatus());
                        Log.d("XMPPChatDemoActivity", "Presence Type: "
                                + entryPresence.getType());
                        Presence.Type type = entryPresence.getType();
                        if (type == Presence.Type.available)
                            Log.d("XMPPChatDemoActivity", "Presence AVIALABLE");
                        Log.d("XMPPChatDemoActivity", "Presence : "
                                + entryPresence);

                    
                 catch (XMPPException ex) 
                    Log.e("XMPPChatDemoActivity", "Failed to log in as "
                            + USERNAME);
                    Log.e("XMPPChatDemoActivity", ex.toString());
                    setConnection(null);
                

                dialog.dismiss();
            
        );
        t.start();
        dialog.show();
    

【问题讨论】:

【参考方案1】:

无法访问主机时抛出ConnectionException。

检查以下内容可能会对您有所帮助。

    忘记在清单文件中添加权限(我经常这样做)

使用权限 android:name="android.permission.INTERNET"

    检查它是否可以从您的 Android 设备 ping im.koderoot.net 添加 e.printStackTrace(); at exception catch 会给出更多提示

我刚刚在im.koderoot.net注册,使用smack4.1.3连接没有问题。

还有一点就是你的代码长期会导致ANR(Application Not Responding),不过这是另外一个话题了。

【讨论】:

我已经在清单文件中拥有互联网权限,我也将 smack 版本从 4.1.0 更改为 4.1.3,但仍然无法正常工作并抛出 java.lang.NoClassDefFoundError: org.jivesoftware.smack.tcp .XMPPTCPConnectionConfiguration 异常 我对程序进行了更改以使其正常运行。我使用了定制的 asmack(asmack-2010.05.07.jar 和 asmack-jse-buddycloud-2010.12.11.jar)并将代码更改为有问题的更新代码

以上是关于如何在安卓上使用 Smack的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 SMACK OMEMO 为群聊或 MUC 生成指纹?

如何使用 smack 库从客户端搜索 ejabberd 服务器上的注册用户?

如何在安卓本地保存聊天记录?

在 android 上使用 smack 4.1.0 的 openfire 问题

如何使用 Smack 确定 MUC 是不是存在?

无法在 android 上使用 igniterealtime smack 无法解析 xpp3:xpp3:1.1.4c