Android基于XMPP Smack Openfire开发IM 【二】获取好友列表

第一步,在openfire后台中创建一些用户,并添加两个组。我这里添加的两个组是:我的聊天小组、篮球。
[img]
Android基于XMPP Smack Openfire开发IM 【二】获取好友列表_第1张图片
[/img]


第二步,编辑组,把用户添加到组中。
[img]
Android基于XMPP Smack Openfire开发IM 【二】获取好友列表_第2张图片
[/img]

[img]
Android基于XMPP Smack Openfire开发IM 【二】获取好友列表_第3张图片
[/img]



第三步,客户端代码如下:
(MainActivity 同上篇,未做修改)
package com.example.openfiretest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText accountEditText;
	private EditText passwordEditText;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		accountEditText = (EditText) findViewById(R.id.username);
		passwordEditText = (EditText) findViewById(R.id.password);

		findViewById(R.id.login).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String account = accountEditText.getText().toString();
				String password = passwordEditText.getText().toString();
				if (account.equals("") || password.equals("")) {
					Toast.makeText(MainActivity.this, "账号或密码不能为空!",
							Toast.LENGTH_SHORT).show();
				} else {
					ClientConServer ccs = new ClientConServer(MainActivity.this);
					boolean b = ccs.login(account, password);
					// 如果登录成功
					if (b) {
						Toast.makeText(MainActivity.this, "登陆成功!",
								Toast.LENGTH_SHORT).show();
						// startActivity(new
						// Intent(LoginActivity.this,MainActivity.class));
						// MainActivity.account=account;
					} else {
						Toast.makeText(MainActivity.this, "登陆失败!",
								Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
	}
}


下面的比上篇的略做修改,添加获取组个数、组名称、组成员名称等代码:
package com.example.openfiretest;

import java.util.Collection;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class ClientConServer {
	private static int PORT=5222;
	private Context context;
	public ClientConServer(Context context){
		this.context=context;

	}
	
	public boolean login(String a,String p){
		ConnectionConfiguration config = new ConnectionConfiguration("192.168.0.124", PORT);
		/** 是否启用安全验证 */
		config.setSASLAuthenticationEnabled(false);
		/** 是否启用调试 */
        //config.setDebuggerEnabled(true);
        /** 创建connection链接 */
		XMPPConnection connection = new XMPPConnection(config);
		try {
			/** 建立连接 */
			connection.connect();
			
			
			/** 登录*/
			connection.login(a, p);
			/** 开启读写线程,并加入到管理类中*/
			//ClientSendThread cst=new ClientSendThread(connection);
			//cst.start();
			//ManageClientThread.addClientSendThread(a, cst);
			
			
			System.out.println("======开始获取组及用户==========");
			Roster roster = connection.getRoster();
			Collection<RosterGroup> entriesGroup = roster.getGroups();
			System.out.println("组的个数:"+entriesGroup.size());
			for(RosterGroup group: entriesGroup){
				Collection<RosterEntry> entries = group.getEntries();
				System.out.println("=========groupName==="+group.getName());
				for (RosterEntry entry : entries) {
					//Presence presence = roster.getPresence(entry.getUser());
					//Log.i("---", "user: "+entry.getUser());
					Log.i("TAG", "name: "+entry.getName());
					System.out.println("组成员名字:"+entry.getName());
					//Log.i("---", "tyep: "+entry.getType());
					//Log.i("---", "status: "+entry.getStatus());
					//Log.i("---", "groups: "+entry.getGroups());
				}
			}
			System.out.println("======结束获取组及用户==========");
			
			
			
			
			
			
			
			
			
			
			return true;
		} catch (XMPPException e) {
			e.printStackTrace();
		}
		return false;
	 }
}




第四步:启动android客户端进行用户登陆:
[img]
Android基于XMPP Smack Openfire开发IM 【二】获取好友列表_第4张图片
[/img]


第五步,查看控制台打印结果如下:
[img]
Android基于XMPP Smack Openfire开发IM 【二】获取好友列表_第5张图片
[/img]

你可能感兴趣的:(android)