java客户端基于XMPP协议连接GTalk

来源:互联网 发布:解决uc屏蔽js广告 编辑:程序博客网 时间:2024/05/22 06:08
XMPPConnection connection = new XMPPConnection("gmail.com");
   
   try
   {
       // Connect
       connection.connect();
       
       // Login with appropriate credentials
       connection.login("******@gmail.com", "*******");
       
       ChatManager chatMgr = connection.getChatManager();
       Chat chat = chatMgr.createChat("******@gmail.com", null);
       chat.sendMessage("hello");


       // Get the user's roster
       Roster roster = connection.getRoster();
       
       // Print the number of contacts
       System.out.println("Number of contacts: " + roster.getEntryCount());


       // Enumerate all contacts in the user's roster
       for (RosterEntry entry : roster.getEntries())
       {
           System.out.println("User: " + entry.getUser());
       }
   }
   catch (XMPPException e)
   {
       // Do something better than this!
       e.printStackTrace();
   }
   finally
   {
    connection.disconnect();
   }
原创粉丝点击