基于 JMSN 写了一个 MsnHelper 。借助getBuddyList(String userName,String password) 方法即可轻松查询所有好友列表。
JMSN 下载地址:http://sourceforge.net/project/showfiles.php?group_id=47932 。使用其中的 msnm.jar 文件即可。
另谁有 GTalk , Yahoo 等其他聊天工具的 Helper ,希望能分享一下 ,谢谢!
package com.pqcc.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import rath.msnm.BuddyGroup;
import rath.msnm.BuddyList;
import rath.msnm.GroupList;
import rath.msnm.MSNMessenger;
/**
*
* @author pqcc
*
*/
public class MsnHelper {
/**
* getGroups(String userName,String password) 查询所有的 msn 分组。
* getBuddyList(String userName,String password) 查询所有好友.(其他查询在线好友也雷同).
* @param args
*/
private static Logger log = Logger.getLogger(MsnHelper.class.getName());
private MSNMessenger msn = null;
public static void main(String[] args)
{
MsnHelper ms = new MsnHelper();
String msnAddress = JOptionPane.showInputDialog("请输入你的MSN");
String password = JOptionPane.showInputDialog("请输入你的密码");
List<String> buddyList = ms.getBuddyList(msnAddress,password);
if(buddyList != null)
{
for(String name:buddyList)
{
log.info("Buddy :"+name);
}
}
}
public void setMsnMessager(String userName,String password)
{
this.msn = new MSNMessenger(userName,password);
}
public MSNMessenger getMsnMessager()
{
return msn;
}
/**
* Login ur msn base on username and password.
* @param userName
* @param password
*/
public void login(String userName,String password)
{
if(msn == null)
setMsnMessager(userName,password);
msn.login();
}
/**
* Get all of ur msn group by username and password. (推荐)
* @param userName
* @param password
* @return all ur msn group .
*/
public List<String> getGroups(String userName,String password)
{
if(msn == null)
setMsnMessager(userName,password);
if(!msn.isLoggedIn())
{
login(userName,password);
}
return getGroups(msn);
}
/**
* Get all of ur buddy by username and password. (推荐)
* @param userName
* @param password
* @return All the buddy
*/
public List<String> getBuddyList(String userName,String password)
{
if(msn == null)
setMsnMessager(userName,password);
if(!msn.isLoggedIn())
{
login(userName,password);
}
return getBuddyList(msn);
}
/**
* Get all of ur buddy group by MSNMessage.(不推荐)
* @param msn
* @return
*/
public List<String> getGroups(MSNMessenger msn)
{
if(msn == null)
return null;
if(!msn.isLoggedIn())
{
log.warn("Please set the MSNMessge");
msn.login();
}
BuddyGroup buddyGroup = msn.getBuddyGroup();
GroupList groupList = buddyGroup.getGroupList();
Iterator groupIterm = groupList.iterator();
List list = null;
while(groupIterm.hasNext())
{
list = (list == null)?new ArrayList():list;
list.add(groupIterm.next().toString());
}
return list;
}
/**
* Get all of ur buddy by MSNMessage.(不推荐,)
* @param msn
* @return
*/
public List<String> getBuddyList(MSNMessenger msn)
{
if(msn == null)
{
log.warn("Please set the MSNMessge");
return null;
}
if(!msn.isLoggedIn())
{
msn.login();
}
BuddyGroup buddyGroup = msn.getBuddyGroup();
BuddyList buddyList = buddyGroup.getForwardList();
List list = null;
// check.
if(buddyList == null || buddyList.size()<1)
return null;
for(int i=0;i<buddyList.size();i++)
{
list = (list == null)?new ArrayList():list;
String fname = buddyList.get(i).getLoginName();
list.add(fname);
}
return list;
}
}
运行结果:
Buddy :***@hotmail.com
Buddy :***@hotmail.com
Buddy :***@hotmail.com
Buddy :***@hotmail.com
Buddy :***@msn.com