如何使用SMACK API和GTALK发送消息?

时间:2013-12-09 10:25:43

标签: java struts smack

我想使用xmpp实现消息传递服务。我正在使用SMACK API和gtalk实现这一目标。

我能够成功连接到服务器。 我还从名册中获取用户列表。但是,当我想在gtalk上向任何用户发送消息时,不会传递消息。

这是我的代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;

import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.SASLAuthentication;

public class MySmackDemo implements MessageListener {

public static String username="abc@mydomain.com";
public static String password="password";
public static String id="";
static ConnectionConfiguration config;
XMPPConnection conn;
public static Chat chat;
public static XMPPConnection connection = new XMPPConnection("gmail.com");
public static boolean free=false;

public static void main(String args[]){

    System.out.println("... in main function .......");
    MySmackDemo mySmackObj = new MySmackDemo();
    mySmackObj.connectTOGTalk();

    mySmackObj.displayBuddyList();
    System.out.println("-----");

            String msg="";

    try {

    mySmackObj.sendMessage("hello from smack", "someone@gmail.com");
    }
    catch(Exception e){
        System.out.println("sending failed");
        e.printStackTrace();
    }

}

private static void connectTOGTalk(){

    try {
        //establish connection between client and server.
        connection.connect();
        System.out.println("Connected to " + connection.getHost());
        //call base class function to get login
        connection.login(username,password);

        System.out.println(connection.isAuthenticated());

        Presence presence = new Presence(Presence.Type.available);
        connection.sendPacket(presence);
        System.out.println("presence is ............" + presence.toXML());

    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

/*
 * Function to send message to specified user
 */
public void sendMessage(String message, String to) throws XMPPException
{
    System.out.println("Message is ......."+message);
    Chat chat = connection.getChatManager().createChat(to, new MySmackDemo());

            System.out.println("Chat obj is ........"+ chat);
}
/**
 * Function to display user list
 */
public void displayBuddyList()
{
Roster roster = connection.getRoster();
roster.setSubscriptionMode(Roster.SubscriptionMode.manual);
Collection<RosterEntry> entries = roster.getEntries();

System.out.println("\n\n" + entries.size() + " buddy(ies):");
for(RosterEntry r:entries)
{
System.out.println(r.getUser());
System.out.println(r.getName());
}
}

public void processMessage(Chat chat, Message message)
{
if(message.getType() == Message.Type.chat)
System.out.println(chat.getParticipant() + " says: " + message.getBody());
}
}

我收到此错误:

java.lang.NullPointerException
at MySmackDemo.main(MySmackDemo.java:65)
sending failed

是否需要额外的设置/配置? 如何发送消息并检查它?

1 个答案:

答案 0 :(得分:1)

在连接之前添加此代码。这将设置config。

<强> ANSWER

   /* this config I missed out */
    ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
    config.setCompressionEnabled(true);
    config.setSASLAuthenticationEnabled(false);
    connection = new XMPPConnection(config);