首先我们要确保openfire 服务器中安装了presence 插件,同时在openfire后台中的服务器设置中进入Presence Service处,设置任何人都可以访问的权限(不过这样设置的话会对安全性有一定影响)。
然后就是代码判断了:
//传入路径格式: String url = "http://localhost:9090/plugins/presence/status?jid=admin@meson.com&type=xml";
public static int IsUserOnLine(String strUrl) {
int state = 0;
//返回值 : 0 - 用户不存在; 1 - 用户在线; 2 - 用户离线
try {
URL oUrl = new URL(strUrl);
URLConnection oConn = oUrl.openConnection();
if (oConn != null) {
BufferedReader oIn = new BufferedReader(new InputStreamReader(oConn.getInputStream()));
if (null != oIn) {
String strFlag = oIn.readLine();
//System.out.println(strFlag);
oIn.close();
if (strFlag.indexOf("type=\"unavailable\"") >= 0) {
state = 2;
}
if (strFlag.indexOf("type=\"error\"") >= 0) {
state = 0;
} else if (strFlag.indexOf("priority") >= 0 || strFlag.indexOf("id=\"") >= 0) {
state = 1;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return state;
} //这是网上copy的代码