Android基於XMPP Smack Openfire下學習開發IM(二)對分組、好友和頭像等一些

下面就一塊兒來學習一下,如何查詢分組和添加分組等!java

1、查詢全部分組 經過Roster來獲取全部分組,Roster能夠經過connection.getRoster()來獲得。服務器

[java] view plaincopyprint? <SPAN style="WHITE-SPACE: pre"> </SPAN>/** * 獲取全部組 *
* @param roster * @return 全部組集合 */
public static List<RosterGroup> getGroups(Roster roster) {
List<RosterGroup> grouplist = new ArrayList<RosterGroup>();
Collection<RosterGroup> rosterGroup = roster.getGroups();
Iterator<RosterGroup> i = rosterGroup.iterator();
while (i.hasNext()) {
grouplist.add(i.next());
}
return grouplist;
}ide

/**
 * 獲取全部組
 * 
 * @param roster
 * @return 全部組集合
 */
public static List<RosterGroup> getGroups(Roster roster) {
	List<RosterGroup> grouplist = new ArrayList<RosterGroup>();
	Collection<RosterGroup> rosterGroup = roster.getGroups();
	Iterator<RosterGroup> i = rosterGroup.iterator();
	while (i.hasNext()) {
		grouplist.add(i.next());
	}
	return grouplist;
}

2、添加分組學習

也同樣經過roster來添加分組,groupName 爲分組名。.net

[java] view plaincopyprint? <SPAN style="WHITE-SPACE: pre"> </SPAN>/** * 添加一個分組 *
* @param roster * @param groupName * @return */
public static boolean addGroup(Roster roster, String groupName) {
try {
roster.createGroup(groupName);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}code

/**
 * 添加一個分組
 * 
 * @param roster
 * @param groupName
 * @return
 */
public static boolean addGroup(Roster roster, String groupName) {
	try {
		roster.createGroup(groupName);
		return true;
	} catch (Exception e) {
		e.printStackTrace();
		return false;
	}
}

3、查詢某個組裏面的全部好友 很簡單不解釋...orm

[java] view plaincopyprint? <SPAN style="WHITE-SPACE: pre"> </SPAN>/** * 獲取某個組裏面的全部好友 *
* @param roster * @param groupName * 組名 * @return */
public static List<RosterEntry> getEntriesByGroup(Roster roster,
String groupName) {
List<RosterEntry> Entrieslist = new ArrayList<RosterEntry>();
RosterGroup rosterGroup = roster.getGroup(groupName);
Collection<RosterEntry> rosterEntry = rosterGroup.getEntries();
Iterator<RosterEntry> i = rosterEntry.iterator();
while (i.hasNext()) {
Entrieslist.add(i.next());
}
return Entrieslist;
}server

/**
 * 獲取某個組裏面的全部好友
 * 
 * @param roster
 * @param groupName
 *            組名
 * @return
 */
public static List<RosterEntry> getEntriesByGroup(Roster roster,
		String groupName) {
	List<RosterEntry> Entrieslist = new ArrayList<RosterEntry>();
	RosterGroup rosterGroup = roster.getGroup(groupName);
	Collection<RosterEntry> rosterEntry = rosterGroup.getEntries();
	Iterator<RosterEntry> i = rosterEntry.iterator();
	while (i.hasNext()) {
		Entrieslist.add(i.next());
	}
	return Entrieslist;
}

4、查詢全部好友信息blog

很簡單rem

[java] view plaincopyprint? <SPAN style="WHITE-SPACE: pre"> </SPAN>/** * 獲取全部好友信息 *
* @param roster * @return */
public static List<RosterEntry> getAllEntries(Roster roster) {
List<RosterEntry> Entrieslist = new ArrayList<RosterEntry>();
Collection<RosterEntry> rosterEntry = roster.getEntries();
Iterator<RosterEntry> i = rosterEntry.iterator();
while (i.hasNext()) {
Entrieslist.add(i.next());
}
return Entrieslist;
}

/**
 * 獲取全部好友信息
 * 
 * @param roster
 * @return
 */
public static List<RosterEntry> getAllEntries(Roster roster) {
	List<RosterEntry> Entrieslist = new ArrayList<RosterEntry>();
	Collection<RosterEntry> rosterEntry = roster.getEntries();
	Iterator<RosterEntry> i = rosterEntry.iterator();
	while (i.hasNext()) {
		Entrieslist.add(i.next());
	}
	return Entrieslist;
}

5、獲取用戶VCard信息

[java] view plaincopyprint? <SPAN style="WHITE-SPACE: pre"> </SPAN>/** * 獲取用戶VCard信息 *
* @param connection * @param user * @return * @throws XMPPException */
public static VCard getUserVCard(XMPPConnection connection, String user)
throws XMPPException {
VCard vcard = new VCard();
vcard.load(connection, user);
return vcard;
}

/**
 * 獲取用戶VCard信息
 * 
 * @param connection
 * @param user
 * @return
 * @throws XMPPException
 */
public static VCard getUserVCard(XMPPConnection connection, String user)
		throws XMPPException {
	VCard vcard = new VCard();
	vcard.load(connection, user);
	return vcard;
}

6、獲取用戶頭像信息

經過Vcard來獲取用戶頭像信息,能夠把 InputStream 轉換爲本身想要的類型,InputStream 轉Drawable

這篇文章裏能夠找到 http://blog.csdn.net/h7870181/article/details/8663760

[java] view plaincopyprint? <SPAN style="WHITE-SPACE: pre"> </SPAN>/** * 獲取用戶頭像信息 *
* @param connection * @param user * @return */
public static Drawable getUserImage(XMPPConnection connection, String user) {
ByteArrayInputStream bais = null;
try {
VCard vcard = new VCard();
// 加入這句代碼,解決No VCard for
ProviderManager.getInstance().addIQProvider("vCard", "vcard-temp",
new org.jivesoftware.smackx.provider.VCardProvider());

vcard.load(connection, user+"@"+connection.getServiceName());  

        if (vcard == null || vcard.getAvatar() == null)  
            return null;  
        bais = new ByteArrayInputStream(vcard.getAvatar());  

    } catch (Exception e) {  
        e.printStackTrace();  
    }  
    if (bais == null)  
        return null;  
    return FormatTools.getInstance().InputStream2Drawable(bais);  
}  

/**
 * 獲取用戶頭像信息
 * 
 * @param connection
 * @param user
 * @return
 */
public static Drawable getUserImage(XMPPConnection connection, String user) {
	ByteArrayInputStream bais = null;
	try {
		VCard vcard = new VCard();
		// 加入這句代碼,解決No VCard for
		ProviderManager.getInstance().addIQProvider("vCard", "vcard-temp",
				new org.jivesoftware.smackx.provider.VCardProvider());

		vcard.load(connection, user+"@"+connection.getServiceName());

		if (vcard == null || vcard.getAvatar() == null)
			return null;
		bais = new ByteArrayInputStream(vcard.getAvatar());

	} catch (Exception e) {
		e.printStackTrace();
	}
	if (bais == null)
		return null;
	return FormatTools.getInstance().InputStream2Drawable(bais);
}

7、添加好友(有、無分組)

[java] view plaincopyprint? <SPAN style="WHITE-SPACE: pre"> </SPAN>/** * 添加好友 無分組 *
* @param roster * @param userName * @param name * @return */
public static boolean addUser(Roster roster, String userName, String name) {
try {
roster.createEntry(userName, name, null);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/** 
 * 添加好友 有分組 
 *  
 * @param roster 
 * @param userName 
 * @param name 
 * @param groupName 
 * @return 
 */  
public static boolean addUser(Roster roster, String userName, String name,  
        String groupName) {  
    try {  
        roster.createEntry(userName, name, new String[] { groupName });  
        return true;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
}  

/**
 * 添加好友 無分組
 * 
 * @param roster
 * @param userName
 * @param name
 * @return
 */
public static boolean addUser(Roster roster, String userName, String name) {
	try {
		roster.createEntry(userName, name, null);
		return true;
	} catch (Exception e) {
		e.printStackTrace();
		return false;
	}
}

/**
 * 添加好友 有分組
 * 
 * @param roster
 * @param userName
 * @param name
 * @param groupName
 * @return
 */
public static boolean addUser(Roster roster, String userName, String name,
		String groupName) {
	try {
		roster.createEntry(userName, name, new String[] { groupName });
		return true;
	} catch (Exception e) {
		e.printStackTrace();
		return false;
	}
}

8、刪除好友

[java] view plaincopyprint? <SPAN style="WHITE-SPACE: pre"> </SPAN>/** * 刪除好友 *
* @param roster * @param userName * @return */
public static boolean removeUser(Roster roster, String userName) {
try {
if (userName.contains("@")) {
userName = userName.split("@")[0];
}

RosterEntry entry = roster.getEntry(userName);  
        System.out.println("刪除好友:" + userName);  
        System.out.println("User." + roster.getEntry(userName) == null);  
        roster.removeEntry(entry);  

        return true;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
}  

/**
 * 刪除好友
 * 
 * @param roster
 * @param userName
 * @return
 */
public static boolean removeUser(Roster roster, String userName) {
	try {
		if (userName.contains("@")) {
			userName = userName.split("@")[0];
		}

		RosterEntry entry = roster.getEntry(userName);
		System.out.println("刪除好友:" + userName);
		System.out.println("User." + roster.getEntry(userName) == null);
		roster.removeEntry(entry);

		return true;
	} catch (Exception e) {
		e.printStackTrace();
		return false;
	}
}

9、查詢用戶

serverDoMain 爲服務器域名

[java] view plaincopyprint? <SPAN style="WHITE-SPACE: pre"> </SPAN>/** * 查詢用戶 *
* @param connection * @param serverDomain * @param userName * @return * @throws XMPPException */
public static List<User> searchUsers(XMPPConnection connection,
String serverDomain, String userName) throws XMPPException {
List<User> results = new ArrayList<User>();
System.out.println("查詢開始..............." + connection.getHost()
+ connection.getServiceName());

UserSearchManager usm = new UserSearchManager(connection);  

    Form searchForm = usm.getSearchForm(serverDomain);  
    Form answerForm = searchForm.createAnswerForm();  
    answerForm.setAnswer("userAccount", true);  
    answerForm.setAnswer("userPhote", userName);  
    ReportedData data = usm.getSearchResults(answerForm, serverDomain);  

    Iterator<Row> it = data.getRows();  
    Row row = null;  
    User user = null;  
    while (it.hasNext()) {  
        user = new User();  
        row = it.next();  
        user.setUserAccount(row.getValues("userAccount").next().toString());  
        user.setUserPhote(row.getValues("userPhote").next().toString());  

        System.out.println(row.getValues("userAccount").next());  
        System.out.println(row.getValues("userPhote").next());  
        results.add(user);  
        // 若存在,則有返回,UserName必定非空,其餘兩個如果有設,必定非空   
    }  
    return results;  
}  

/**
 * 查詢用戶
 * 
 * @param connection
 * @param serverDomain
 * @param userName
 * @return
 * @throws XMPPException
 */
public static List<User> searchUsers(XMPPConnection connection,
		String serverDomain, String userName) throws XMPPException {
	List<User> results = new ArrayList<User>();
	System.out.println("查詢開始..............." + connection.getHost()
			+ connection.getServiceName());

	UserSearchManager usm = new UserSearchManager(connection);

	Form searchForm = usm.getSearchForm(serverDomain);
	Form answerForm = searchForm.createAnswerForm();
	answerForm.setAnswer("userAccount", true);
	answerForm.setAnswer("userPhote", userName);
	ReportedData data = usm.getSearchResults(answerForm, serverDomain);

	Iterator<Row> it = data.getRows();
	Row row = null;
	User user = null;
	while (it.hasNext()) {
		user = new User();
		row = it.next();
		user.setUserAccount(row.getValues("userAccount").next().toString());
		user.setUserPhote(row.getValues("userPhote").next().toString());

		System.out.println(row.getValues("userAccount").next());
		System.out.println(row.getValues("userPhote").next());
		results.add(user);
		// 若存在,則有返回,UserName必定非空,其餘兩個如果有設,必定非空
	}
	return results;
}

10、修改用戶頭像

[java] view plaincopyprint? <SPAN style="WHITE-SPACE: pre"> </SPAN>/** * 修改用戶頭像 *
* @param connection * @param f * @throws XMPPException * @throws IOException */
public static void changeImage(XMPPConnection connection, File f)
throws XMPPException, IOException {

VCard vcard = new VCard();  
    vcard.load(connection);  

    byte[] bytes;  

    bytes = getFileBytes(f);  
    String encodedImage = StringUtils.encodeBase64(bytes);  
    vcard.setAvatar(bytes, encodedImage);  
    vcard.setEncodedImage(encodedImage);  
    vcard.setField("PHOTO", "<TYPE>image/jpg</TYPE><BINVAL>" + encodedImage  
            + "</BINVAL>", true);  

    ByteArrayInputStream bais = new ByteArrayInputStream(vcard.getAvatar());  
    FormatTools.getInstance().InputStream2Bitmap(bais);  

    vcard.save(connection);  
}
相關文章
相關標籤/搜索