Note that there are some explanatory texts on larger screens.

plurals
  1. POCan not receive chat messages in Android using XMPP and aSmack
    text
    copied!<p>I am writing a chat client in Android using the XMPP protocol. I have used the asmack.jar as provided by <a href="http://asmack.freakempire.de/" rel="nofollow">http://asmack.freakempire.de/</a>. The implementation works in plain Java (using smack.jar) which I have tested. But in Android, I can only send messages to the other client (he uses pidgin) and cannot receive messages from him. The app successfully connects to the server, logs in and appears online but simply doesn't receive any message. </p> <p>My <code>processMessage()</code> never gets called nor does <code>chatCreated()</code> when a message is send to my client.</p> <p>My Activity class:</p> <pre><code>package com.example.basicchat; import org.jivesoftware.smack.AndroidConnectionConfiguration; import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.ChatManager; import org.jivesoftware.smack.ChatManagerListener; import org.jivesoftware.smack.ConnectionListener; import org.jivesoftware.smack.MessageListener; import org.jivesoftware.smack.SmackAndroid; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.packet.Message; import android.app.Activity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SmackAndroid.init(this); MainActivity2 xmppClient= new MainActivity2("jabber.org", 5222); try { xmppClient.login(); } catch (XMPPException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public class MainActivity2 implements MessageListener, ChatManagerListener, ConnectionListener { private String server; private int port; public MainActivity2( String server, int port ) { super(); this.server = server; this.port = port; } private XMPPConnection connection = null; public void login() throws XMPPException { String username = "my_user"; String password = "xxxxxxxx"; login(username, password); } private void login(String userName, String password) throws XMPPException { AndroidConnectionConfiguration config = new AndroidConnectionConfiguration(server, port); connection = new XMPPConnection(config); connection.connect(); connection.addConnectionListener(this); connection.login(userName, password); ChatManager chatManager = connection.getChatManager(); chatManager.addChatListener(this); Toast.makeText(MainActivity.this,"listener set", Toast.LENGTH_SHORT).show(); // sendMessage("helloooo","command_server@jabber.org"); /* this mssg is sent */ } private void sendMessage(String message, String to) throws XMPPException { Chat chat = connection.getChatManager().createChat(to, this); chat.sendMessage(message); } public void disconnect() { connection.disconnect(); } /** never gets called **/ @Override public void processMessage(Chat chat, Message message) { /*********** Handle Request and construct response ******************/ Toast.makeText(MainActivity.this,"mssg: "+message.getBody(), Toast.LENGTH_SHORT).show(); switch (message.getType()) { case chat: String jsonData = (null==message.getBody())?"":message.getBody(); System.out.println(jsonData); break; case error: break; case groupchat: break; case headline: break; case normal: break; } } @Override public void chatCreated(Chat arg0, boolean arg1) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,"Chat Created!", Toast.LENGTH_SHORT).show(); if (!arg1) arg0.addMessageListener(this); } @Override public void connectionClosed() { // TODO Auto-generated method stub } @Override public void connectionClosedOnError(Exception arg0) { // TODO Auto-generated method stub } @Override public void reconnectingIn(int arg0) { // TODO Auto-generated method stub } @Override public void reconnectionFailed(Exception arg0) { // TODO Auto-generated method stub } @Override public void reconnectionSuccessful() { // TODO Auto-generated method stub } } </code></pre> <p>}</p> <p>Manifest file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.basicchat" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="8" /&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt; &lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name="com.example.basicchat.MainActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload