【问题标题】:Android chat using Smack API and talk.goolgle.com server使用 Smack API 和 talk.goolgle.com 服务器的 Android 聊天
【发布时间】:2014-03-12 23:16:58
【问题描述】:

我想使用 smack api 和 talk.google.com 服务器创建一个 android 聊天应用程序,但问题是当我使用 gmail id 和密码创建到 Smack API 的连接并检查我的用户时名称使用“connection.getuser()”我的 id 以以下格式显示“anything@gmail.com/Smack_(some smack id)”。现在如果我发送从我的应用程序使用另一部手机发送到“anything@gmail.com”的消息未收到,但如果我将其发送到“anything@gmail.com/Smack_(相同的 smack id)”,则正确收到了。

附加的 smack id 是随机的,每次连接时都会发生变化,因此我无法通过任何方式获取它,因此无法发送消息,那么我该如何解决这个问题。

我的 ChatActivity 的代码是:

    package com.prince.chat;

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.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.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ChatActivity extends Activity {

    public static final String HOST = "talk.google.com";
    public static final int PORT = 5222;
    public static final String SERVICE = "gmail.com";
    public static  String USERNAME ;
    public static  String PASSWORD ;

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

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

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

        Intent i = getIntent();
        USERNAME= i.getStringExtra("email");
        PASSWORD =i.getStringExtra("password");

        // 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 establised with 
     * the XMPP server
     */
    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 {
            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",  "[SettingsDialog] Connected to "+connection.getHost());
                } catch (XMPPException ex) {
                    Log.e("XMPPChatDemoActivity",  "[SettingsDialog] Failed to connect to "+ connection.getHost());
                    Log.e("XMPPChatDemoActivity", ex.toString());
                    setConnection(null);
                }
                try {
                    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();
    }



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

}

【问题讨论】:

  • 听起来很奇怪,消息是广播发送的,所以每个客户端都会收到消息,也许你可以发布你的代码。我也写过类似的应用程序。
  • @user1242027 我也添加了代码
  • @Prince 你能在 android 中使用 java smack 吗?我想我必须使用 Asmack 才能在 android 中使用 Xmpp。我正在尝试在 android 中实现 gcm Xmpp 客户端,以便我可以发送广播消息to gcm CCS(xmpp).希望你能尽快回复这个评论,谢谢:)
  • @user3523641 我使用了 Asmack,我还没有尝试过很多 GCM,但是对于广播,您可以使用基于 Smack 的 quickblox.com API 或类似的东西。好处是您的所有代码都将在 JAVA 中,您无需担心后端。
  • 谢谢你的回答差异很大,你能建议我有什么方法可以在我的安卓手机中实现 gcm CCS(xmpp),我正在处理你上面写的代码在安卓手机中实现gcm xmpp客户端。

标签: java android smack asmack


【解决方案1】:

您可以在连接时将资源 id 设置为某个已知值,因此不会使用生成的 id(除非服务器更改它,我相信如果它愿意,它可以这样做)。

【讨论】:

  • 用户可以使用其他XMPP客户端软件如spark,如何处理? @罗宾
猜你喜欢
  • 1970-01-01
  • 2011-10-01
  • 2014-04-24
  • 2014-06-19
  • 1970-01-01
  • 1970-01-01
  • 2011-12-05
  • 2013-09-07
  • 1970-01-01
相关资源
最近更新 更多