smack 多人聊天获取聊天室列表

今天开始写聊天室模块了,粘贴部分代码处理供大家参考。

 public void chatRoom() {//加载聊天室信息
  listDiscoverItems = new ArrayList<ChatRoomListInfo>();
  pd = new ProgressDialog(MyFriendsActivity.this);
  pd.setTitle("提示");
  pd.setMessage("正在更新列表");
  pd.show();
  chatroom_list = (ListView) this.findViewById(R.id.chatroom_list);

  new Thread(new Runnable() {
   public void run() {
    try {
     init();//初始化房间列表
    } catch (XMPPException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    Message msg = new Message();
    msg.what = 10;
    handler.sendMessage(msg);
   }
  }).start();
  chatroom_list.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> arg0, View view,
     int position, long id) {
    // 获取用户,跳到会议
    String jid = listDiscoverItems.get(position).getJid();
    intent = new Intent(MyFriendsActivity.this,
      ChatRoom2Activity.class);
    intent.putExtra("jid", jid);
    System.out.println("------jid--------"+jid);
    intent.putExtra("action", "join");
    startActivity(intent);
   }
  });

 }

 

 /**
  * 初始化房间列表
  */
 public void init() throws XMPPException {
  XMPPConnection conn = XmppUtils.getInstance().getConnection();
  try {
   // 查找服务 
   List<String> col = getConferenceServices(conn.getServiceName(),
     conn);
   for (Object acol : col) {
    String service = (String) acol;
    // 查询服务器上的聊天室   
    Collection<HostedRoom> rooms = MultiUserChat.getHostedRooms(
      conn, service);
    for (HostedRoom room : rooms) {
     // 查看Room消息   
     RoomInfo roomInfo = MultiUserChat.getRoomInfo(conn,
       room.getJid());
     if (roomInfo != null) {

      ChatRoomListInfo info = new ChatRoomListInfo();

//      if (roomName.equals(Utils.getJidToUsername(roomInfo
//        .getRoom()))) {
       info.setRoomName(Utils.getJidToUsername(roomInfo
         .getRoom()));
       listDiscoverItems.add(info);
       System.out
         .println("----1-----房间信息--------"
           + Utils.getJidToUsername(roomInfo
             .getRoom()));
    //  }
     }
    }

   }

  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

/**
 *
 * 获取服务
 * **/
 private List<String> getConferenceServices(String server,
   XMPPConnection conn) throws Exception {
  List<String> answer = new ArrayList<String>();
  ServiceDiscoveryManager discoManager = ServiceDiscoveryManager
    .getInstanceFor(conn);
  discoManager = new ServiceDiscoveryManager(conn);
  DiscoverItems items = discoManager.discoverItems(server);
  for (Iterator<DiscoverItems.Item> it = items.getItems(); it.hasNext();) {
   DiscoverItems.Item item = (DiscoverItems.Item) it.next();
   if (item.getEntityID().startsWith("conference")
     || item.getEntityID().startsWith("private")) {
    answer.add(item.getEntityID());
   } else {
    DiscoverInfo info = discoManager.discoverInfo(item
      .getEntityID());
    if (info.containsFeature("http://jabber.org/protocol/muc")) {
     answer.add(item.getEntityID());
    }

   }

  }
  return answer;
 }

 

展开阅读全文

没有更多推荐了,返回首页