最近在做IM读取联系人的工作,主要包括MSN和Gtalk,研究了以下smack和jml的文档,发现还是比较容易的,虽然代码很少,但是却颇费了一般周折
代码如下:
import java.util.ArrayList;
import java.util.List;
import net.sf.jml.MsnContact;
import net.sf.jml.MsnList;
import net.sf.jml.MsnMessenger;
import net.sf.jml.MsnUserStatus;
import net.sf.jml.event.MsnContactListAdapter;
import net.sf.jml.impl.MsnMessengerFactory;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
public class IMRosterService {
private String user;
private String password;
private String type;
public IMRosterService(String user, String password, String type) {
this .user = user;
this .password = password;
this .type = type;
}
public List < ContactFriend > getContactList() {
if (type.equals( " msn " ))
return getMsnRoster();
return getGtalkRoster();
}
private List < ContactFriend > getMsnRoster() {
MsnMessenger messenger = null ;
final List < ContactFriend > list = new ArrayList < ContactFriend > ();
try {
messenger = MsnMessengerFactory.createMsnMessenger(user, password);
messenger.getOwner().setInitStatus(MsnUserStatus.BUSY);
messenger.addContactListListener( new MsnContactListAdapter() {
public void contactListInitCompleted(MsnMessenger messenger) {
MsnContact[] contacts = messenger.getContactList().getContactsInList(MsnList.AL);
for ( int i = 0 ; i != contacts.length; i ++ ) {
String email = contacts[i].getEmail().getEmailAddress();
String name = contacts[i].getDisplayName();
list.add( new ContactFriend(email, name));
}
}
});
messenger.login();
// must wait until result
Thread.sleep( 5000 );
} catch (Exception e) {
e.printStackTrace();
} finally {
messenger.logout();
}
return list;
}
private List < ContactFriend > getGtalkRoster() {
List < ContactFriend > list = new ArrayList < ContactFriend > ();
ConnectionConfiguration connectionConfig = new ConnectionConfiguration( " talk.google.com " , 5222 , " gmail.com " );
XMPPConnection connection = new XMPPConnection(connectionConfig);
try {
connection.connect();
connection.login(user, password);
Roster roster = connection.getRoster();
if (roster != null ) {
for (RosterEntry entry : roster.getEntries()) {
String email = entry.getUser();
String name = entry.getName();
if (name == null ) {
name = email;
}
ContactFriend friend = new ContactFriend(email, name);
list.add(friend);
}
}
} catch (XMPPException e) {
e.printStackTrace();
} finally {
if (connection.isConnected())
connection.disconnect();
}
return list;
}
}
前几天发现sourceforge被封了,感觉影响还不大,可是真正工作的时候就感觉到了,smack还好,可是jml的src和jar都在上面,后来发现大部分的时间是在找jar包,这里也把上面用到的jar包的下载地址贴出来了。
我是通过Google快照才找到jml的下载地址的: http://203.208.37.104/search?q=cache:y-nIvXx4G_IJ:sourceforge.net/project/showfiles.php%3Fgroup_id%3D118829+Java+MSN+Messenger+Library+jar&hl=zh-CN&ct=clnk&cd=2&gl=cn&st_usg=ALhdy2_lUuTxxQy80mgPaNFQ6wg76W1v6w
在里面可以找到下载文件: http://downloads.sourceforge.net/java-jml/jml-1.0b2.jar?modtime=1186257954&big_mirror=0,用迅雷就好了,jml又要用到另外一个组件:cindy,作者同为crmky,而且必须用cindy-2.4.4,还好找到了sourceforge的镜像,下载地址为: http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/c/ci/cindy/cindy-2.4.4.jar
后面有时间再继续研究openfire的IM-Gateway吧,代码的确写的比较优雅