【问题标题】:Does not send message using XMPP不使用 XMPP 发送消息
【发布时间】:2014-02-11 12:20:05
【问题描述】:
public class MainActivity extends Activity {
    Button reply;
    EditText say; 
    String username;
    private ListView listView;
   XMPPConnection connection=HomeScreen.connection;

    @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.chatbos_listview);
      listView = (ListView) findViewById(R.id.listview);
      Intent i=getIntent();
      username=i.getStringExtra("username");
      say=(EditText)findViewById(R.id.reply);
      reply=(Button)findViewById(R.id.replybtn);    
      reply.setOnClickListener(new OnClickListener() {
          String text = say.getText().toString();

          @Override
            public void onClick(View view) {
              Message msg = new Message(username, Message.Type.chat);
                msg.setBody(text);              
                if (connection != null) {
                    connection.sendPacket(msg);
                    //messages.add(text);
                    //setListAdapter();
                }
                System.out.println(connection);

            }
        });
     }

基本上它是一个聊天应用程序。我在启动活动中创建了连接,在此活动中,我从启动活动获取连接并将数据发送给用户。但它不起作用。请指导我。

【问题讨论】:

  • “它不起作用”是什么意思?
  • 我无法发送消息
  • @selvin 它没有显示任何异常。我已成功连接但我不知道问题出在哪里。

标签: android connection xmpp


【解决方案1】:

将此代码放在 onclick 方法中 字符串文本 = say.getText().toString();

【讨论】:

  • 只是一个小错误。
【解决方案2】:

在发送消息之前,您必须与要向其发送消息的其他用户创建聊天,请阅读此处:http://www.igniterealtime.org/builds/smack/docs/latest/documentation/messaging.html

也在 Asyntask 中发送消息而不是在主 UI 上,否则你的应用会崩溃。

【讨论】:

  • 我添加了用户。我从另一个活动中获得了用户名。用户名=i.getStringExtra("用户名");
  • 你实现包监听了吗?
  • 是的。我还通过在控制台上打印它们来确认连接和用户名值。它工作正常。但仍然有发送问题。
猜你喜欢
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
  • 2013-10-28
  • 2010-10-28
  • 2011-05-31
  • 2019-09-06
  • 2011-05-18
  • 1970-01-01
相关资源
最近更新 更多