-1
我想使用xmpp實現消息傳遞服務。我正在使用SMACK API和gtalk來實現這一點。如何使用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="[email protected]";
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", "[email protected]");
}
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
是否有任何額外的設置/配置所需? 如何發送消息並檢查它?