2015-05-31 271 views
2

我想通過android中的SMack庫發送和接收XMPP消息。但未能做到這一點。即使我成功連接到服務器,並獲得在線用戶。但無法發送或接收短信。請提出任何解決方案。無法發送和接收XMPP消息

代碼: XMPP建立連接:

try { 


      ConnectionConfiguration config = new ConnectionConfiguration("chat.spectratech.in"); 
      config.setTruststorePath("/system/etc/security/cacerts.bks"); 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 
       config.setTruststoreType("AndroidCAStore"); 
       config.setTruststorePassword(null); 
       config.setTruststorePath(null); 
      } else { 
       config.setTruststoreType("BKS"); 
       String path = System.getProperty("javax.net.ssl.trustStore"); 
       if (path == null) 
        path = System.getProperty("java.home") + File.separator + "etc" 
         + File.separator + "security" + File.separator 
         + "cacerts.bks"; 
       config.setTruststorePath(path); 
      } 
      mXmppConnection = new XMPPConnection(config); 
      mXmppConnection.connect(); 
      mXmppConnection.login(USERNAME, PASSWORD); 

      chatApp.setmXmppConnection(mXmppConnection); 
     } 
     catch (final XMPPException e) { 
      Log.e(TAG, "Could not connect to Xmpp server.", e); 
      return; 
     } 

     if (!mXmppConnection.isConnected()) { 
      Log.e(TAG, "Could not connect to the Xmpp server."); 
      return; 
     } 

     Log.i(TAG, "Yey! We're connected to the Xmpp server!"); 
     Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show(); 
     mXmppConnection.getChatManager().addChatListener(new ChatManagerListener() { 

      @Override 
      public void chatCreated(final Chat chat, final boolean createdLocally) { 
       if (!createdLocally) { 
        chat.addMessageListener(new MyMessageListener()); 
       } 
      } 
     }); 

發送和接收消息:

if (app.getmXmppConnection() != null) { 
      // Add a packet listener to get messages sent to us 
      PacketFilter filter = new MessageTypeFilter(Message.Type.chat); 
      app.getmXmppConnection().addPacketListener(new PacketListener() { 
       @Override 
       public void processPacket(Packet packet) { 
        Message message = (Message) packet; 
        if (message.getBody() != null) { 
         String fromName = StringUtils.parseBareAddress(message 
           .getFrom()); 
         Log.i("XMPPChatDemoActivity", "Text Recieved " 
           + message.getBody() + " from " + fromName); 
         msgs.add(fromName + ": " + message.getBody()); 
         //messages.add(fromName + ":"); 
         //messages.add(message.getBody()); 
         // Add the incoming message to the list view 
         mHandler.post(new Runnable() { 
          public void run() { 
           adapter.notifyDataSetChanged(); 
          } 
         }); 
        } 
       } 
      }, filter); 
     } 

     btnSend.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       String text = edMsgs.getText().toString(); 
       Message msg = new Message(to, Message.Type.chat); 
       msg.setBody(text); 

       if(app.getmXmppConnection()!=null){ 
        app.getmXmppConnection().sendPacket(msg); 
        msgs.add(text); 
        adapter.notifyDataSetChanged(); 
       } 

      } 
     }); 

回答

1

首先,您需要登錄到服務器。

鏈接Class Message

發送消息:XMPPConnection類用於發送命令sendpacket。

String message = "Hello friend"; 
Message msg = new Message(toUserId, Message.Type.chat); 
        msg.setBody(message); 
        connection.sendPacket(msg); 

接收消息:

我使用PacketFilter類來recive只是聊天消息。 我在使用XMPPConnection類添加列表程序時消息。

PacketFilter chatFilter = new MessageTypeFilter(Message.Type.chat);  
connection.addPacketListener(chatPacketListener, chatFilter);   

PacketListener chatPacketListener = new PacketListener() { 

     @Override 
     public void processPacket(Packet packet) { 
      try { 

       Message message = (Message) packet; 
       String body = message.getBody(); 
       String from = StringUtils.parseBareAddress(message.getFrom()); 

      } catch (Exception e) {} 
     } 
    };  

我希望它能幫助你。