无法将简单的Java Smack连接到Google Talk

时间:2014-01-28 11:17:34

标签: java android

我想在我的Android应用程序中发送和接收gmail聊天。

每次HostConnection::get() New Host Connection established 0x2a1bfc38, tid 16683都记录在案。仅此而已。

以下是我的代码。

 XMPPConnection xmpp = new XMPPConnection("gmail.com");
    try {
      xmpp.connect();

      // for other jabber accounts, truncate after the @
      //xmpp.login("username", "password"); 

      // for gtalk / gmail, include the @
      xmpp.login("test04@gmail.com", "***");

      Log.v("nirav", " connect to " + xmpp.getHost());

      ChatManager chatmanager = xmpp.getChatManager();

      Chat newChat = chatmanager.createChat("test04@gmail.com", new MessageListener() {
          // THIS CODE NEVER GETS CALLED FOR SOME REASON
          public void processMessage(Chat chat, Message message) {
            try {
              Log.v(TAG, "Got:" + message.getBody());
              chat.sendMessage(message.getBody());
            } catch (XMPPException e) {
              Log.v(TAG, "Couldn't respond:" + e);
            }
            Log.v(TAG, message.toString());
          }
        });

      try
      {
          newChat.sendMessage("adsd");
      }
      catch(Exception e)
      {
          Log.v(TAG, e.getMessage().toString());
      }

   // Accept only messages from friend@gmail.com
      PacketFilter filter 
          = new AndFilter(new PacketTypeFilter(Message.class), 
                          new FromContainsFilter("test@gmail.com"));

      // Collect these messages
      PacketCollector collector = xmpp.createPacketCollector(filter);

      while(true) {
          Packet packet = collector.nextResult();

          if (packet instanceof Message) {
            Message msg = (Message) packet;
            // Process message
            Log.v(TAG, "Got message:" + msg.getBody());
          }
        }

    }
    catch(Exception e)
    {

    }

jar文件列表:

jar file list for this project

1 个答案:

答案 0 :(得分:1)

以下是gmail聊天应用程序的解决方案代码,用于在您的应用程序中发送和接收文本。

您应该尝试使用此代码。 希望对你有所帮助。

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 (xmpp != null) {
                xmpp.sendPacket(msg);
                messages.add(xmpp.getUser() + ":");
                messages.add(text);
                setListAdapter();
            }
        }
    });

    connect();
}
public void setConnection(XMPPConnection connection) {
    this.xmpp = connection;
    if (xmpp != null) {
        // Add a packet listener to get messages sent to us
        PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
        xmpp.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);
}
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 xmpp = new XMPPConnection(connConfig);

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

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

                Roster roster = xmpp.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();
}