XMPP好友列表显示

Posted

技术标签:

【中文标题】XMPP好友列表显示【英文标题】:XMPP friendlist display 【发布时间】:2013-11-11 18:14:16 【问题描述】:

实际上我想在 textview 或 listview 中显示朋友列表中的名称,请建议任何好的方式来表示它。我使用了以下代码,但它不起作用。确切地说,我正在寻找显示在线离线状态的好友列表的解决方案。请帮助我。

    public void connect() 

    final ProgressDialog dialog = ProgressDialog.show(this,
            "Connecting...", "Please wait...", false);

    Thread t = new Thread(new Runnable() 

        @Override
        public void run() 
            // Create a connection
            ConnectionConfiguration connConfig = new ConnectionConfiguration(
                    HOST, PORT, SERVICE);
            XMPPConnection connection = new XMPPConnection(connConfig);
            try 
                connection.connect();
                Log.i("XMPPChatDemoActivity",
                        "Connected to " + connection.getHost());
             catch (XMPPException ex) 
                Log.e("XMPPChatDemoActivity", "Failed to connect to "
                        + connection.getHost());
                Log.e("XMPPChatDemoActivity", ex.toString());

            

            try 
                // SASLAuthentication.supportSASLMechanism("PLAIN", 0);
                connection.login(USERNAME, PASSWORD);
                Log.i("XMPPChatDemoActivity",
                        "Logged in as " + connection.getUser());

                // Set the status to available
                Presence presence = new Presence(Presence.Type.available);
                connection.sendPacket(presence);
                Roster roster = connection.getRoster();
                Collection<RosterEntry> entries = roster.getEntries();
                sb = new StringBuffer();
                for (RosterEntry entry : entries) 
                    Presence entryPresence = roster.getPresence(entry
                            .getUser());
                    String number1 = entry.getUser();

                    String name1 = entry.getName();
                    String status = entryPresence.getStatus();
                    String TYpe;
                    Presence.Type type = entryPresence.getType();
                    if (type == Presence.Type.available)
                        TYpe="available";
                        else
                        TYpe="unavailable";
                        
                        sb.append( "\nName:---"+number1 );
                        Log.e(number1, "names");
                        sb.append("\n----------------------------------");
                

                name.setText(sb);

            

                catch (XMPPException ex) 
                    Log.e("XMPPChatDemoActivity", "Failed to log in as "
                            + USERNAME);
                    Log.e("XMPPChatDemoActivity", ex.toString());

                 

                dialog.dismiss();

        


    );
        t.start();
        dialog.show();




【问题讨论】:

请尽快提出建议...谢谢 任何人都会回复或不回复。没有人可以提供帮助.. 什么不完全有效?无法建立连接? 连接很好..问题是无法查看好友列表..当我把它放在“Log()”中时..它工作正常..怎么办 你如何显示它?向我们展示您的 xml 和用于显示数据的相关代码 【参考方案1】:
public class Done extends Activity
StringBuilder sb;
List<RowItems> rowItems;
CustomListViewAdapter adapter;
private ListView lst;
String TYpe;
Bitmap img ;

  List<String> myVal = new ArrayList<String>() ;
@Override
protected void onCreate(Bundle savedInstanceState) 

    super.onCreate(savedInstanceState);
    setContentView(R.layout.lists);
    lst=(ListView)findViewById(R.id.listView1);
    new MyAsynch().execute();


    private void populate() 

    rowItems=new ArrayList<RowItems>();

    Roster roster = connection.getRoster();
    Collection<RosterEntry> entries = roster.getEntries();
     ProviderManager.getInstance().addIQProvider("vCard", "vcard-temp",
             new VCardProvider());
     VCard card = null;
    for (RosterEntry entry : entries) 
         card = new VCard();
         try 
             card.load(connection, entry.getUser());
          catch (Exception e) 
             e.printStackTrace();
         
        String user=entry.getUser();

        String name=entry.getName();

        Presence entryPresence = roster.getPresence(entry
                .getUser());
        Presence.Type type = entryPresence.getType();
        if (type == Presence.Type.available)


        byte[] imgs = card.getAvatar();
        if (imgs != null) 
            int len = imgs.length;
            img = BitmapFactory.decodeByteArray(imgs, 0, len);
        
        RowItems p=new RowItems(user,name,img);
        rowItems.add(p);
    



   

lst.setOnItemClickListener(new OnItemClickListener()       
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int arg2, long arg3) 
        TextView textViewOne = (TextView)arg1.findViewById(R.id.desc);
        TextView textViewTwo = (TextView)arg1.findViewById(R.id.title);
     String item = textViewOne.getText().toString();
     String items = textViewTwo.getText().toString();
    Toast.makeText(getApplicationContext(), item +"\n"+ items,
              Toast.LENGTH_LONG).show(); 
    Intent intent = new Intent(Done.this, Chat.class);
    intent.putExtra("contentOne", item);
    intent.putExtra("contentTwo", items);
    startActivity(intent);
    

);

    private class MyAsynch extends AsyncTask<Void, Void, Void>
     ProgressDialog dialog;


    @Override
    protected void onPreExecute() 
        // TODO Auto-generated method stub
        super.onPreExecute();
        dialog = ProgressDialog.show(Done.this,
                "Loading...", "Getting friendlist", true);
    
    @Override
    protected Void doInBackground(Void... arg0) 

        populate();
        return null;
    

    @Override
    protected void onPostExecute(Void result) 

        super.onPostExecute(result);
        adapter= new CustomListViewAdapter(getApplicationContext(), R.layout.activity_listitem, rowItems);
        lst.setAdapter(adapter);
        dialog.dismiss();
           
           
       

【讨论】:

请给你的适配器类

以上是关于XMPP好友列表显示的主要内容,如果未能解决你的问题,请参考以下文章

XMPP框架下微信项目总结刷新好友列表(删除,添加好友)

如何在ios中使用xmpp获取在线好友?

ios怎样在xmpp自带的数据库里面插入数据

iOS:XMPPRoster 也获取邀请好友列表

使用 XMPP 更改好友显示名称(别名)

ios qqq好友列表滑动怎么实现