2010-07-19 52 views
1

嗨,我是新的Java。它給我很大的壓力。我需要與smack api和openfire服務器聊天。爲此我的Java代碼是低於添加好友與smack API和openfire服務器的問題

import java.util.*; 
import java.io.*; 

import org.jivesoftware.smack.Chat; 
import org.jivesoftware.smack.ConnectionConfiguration; 
import org.jivesoftware.smack.MessageListener; 
import org.jivesoftware.smack.Roster; 
import org.jivesoftware.smack.RosterEntry; 
import org.jivesoftware.smack.XMPPConnection; 
import org.jivesoftware.smack.XMPPException; 
import org.jivesoftware.smack.packet.Message; 

public class RunJabberSmackAPI implements MessageListener{ 

    XMPPConnection connection; 

    public void login(String userName, String password) throws XMPPException { 
     ConnectionConfiguration config = new ConnectionConfiguration("127.0.0.1 ",5222,"localhost"); 
     connection = new XMPPConnection(config); 

     connection.connect(); 
     connection.login(userName, password); 
    } 

    public void sendMessage(String message, String to) throws XMPPException { 
     Chat chat = connection.getChatManager().createChat(to, this); 
     chat.sendMessage(message); 
    } 

    public void displayBuddyList() 
    { 
     Roster roster = connection.getRoster(); 
     Collection<RosterEntry> entries = roster.getEntries(); 

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

    public void disconnect() { 
     connection.disconnect(); 
    } 

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

    public static void main(String args[]) throws XMPPException, IOException { 
     // declare variables 
     RunJabberSmackAPI c = new RunJabberSmackAPI(); 
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     String msg; 

     // turn on the enhanced debugger 
     XMPPConnection.DEBUG_ENABLED = true; 

     // Enter your login information here 
     c.login("admin", "admin"); // I created this user with openfire. 

     c.displayBuddyList(); 

     System.out.println("-----"); 
     System.out.println("Who do you want to talk to? - Type contacts full email address:"); 
     String talkTo = br.readLine(); 

     System.out.println("-----"); 
     System.out.println("All messages will be sent to " + talkTo); 
     System.out.println("Enter your message in the console:"); 
     System.out.println("-----\n"); 

     while(!(msg=br.readLine()).equals("bye")) { 
     c.sendMessage(msg, talkTo); 
     } 

     c.disconnect(); 
     System.exit(0); 
    } 
} 

我在我的電腦上運行此代碼兩次。每個用戶都是個人用戶。我通過添加公雞將這兩個用戶添加爲openfire的朋友。 但是,當他們通過運行上面的java代碼登錄時,他們會發送可用的存在。但他們不能將他們的存在發送給對方。相反,他們從他們的好友處收到兩條錯誤消息。

First error message : www.freeimagehosting.net/image.php?eac15f606a.jpg 
Second error message : www.freeimagehosting.net/image.php?b827058d07.jpg 

我不知道我的代碼有什麼問題。我真的需要很快解決這個問題。我也發佈了這個問題的其他論壇,但無法找到任何答案。所以如果任何人有任何解決方案,這將是一個非常大的幫助。謝謝。

+0

有人請幫忙。我需要快速解決這個問題:( – Tahlil 2010-07-26 05:03:33

回答

0

在IgniteRealtime網絡的許多線程中,你可以看到你需要讓Smack異步檢索Roster,所以要麼改變displayBuddyList()來改爲使用RosterListener,要麼你只需使用Thread.sleep(5000)登錄和displayBuddyList()函數(如果您不想使用監聽器,建議使用它),讓它有一些時間用更新的狀態填充名單。

+0

感謝您的回覆:)我已經找到答案並解決了它:D – Tahlil 2011-07-15 06:36:46

+0

@kalkin我有同樣的問題,我正在尋找過去4天的soln。你能幫我麼 ?? – 2012-10-11 08:53:08

+0

@kalkin我必須解決它非常快的兄弟:( – 2012-10-11 08:53:37