I am trying to create a basic message passing infrastructure using XMPP servers and have succeeded in doing so quite elegantly using Python. Small, predefined messages are being successfully sent and received on Python. Now the next task is to send similar messages from and Android phone. Note I really do not need the full chat client functionality that is available from Xabber style clients.
I am new to Android and have not really worked on Java, Eclipse. Nevertheless, I have taken a basic "Hello World" program (the blank activity template) in Android and have "enhanced" with some basic XMPP code taken from igniterealtime website. So the MainActivity.java program looks like this :
package com.yantrajaal.pm1324;
import java.io.IOException;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword("userid", "xxxxxx");
//configBuilder.setResource("SomeResource");
configBuilder.setServiceName("adastra.re");
AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
// Connect to the server
try {
connection.connect();
} catch (SmackException | IOException | XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Log into the server
try {
connection.login();
} catch (SmackException | IOException | XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
the smack libraries that i have included are
/home/hduser/workspace/PM1324/libs/smack-tcp-4.1.4.jar
/home/hduser/workspace/PM1324/libs/smack-android-4.1.4.jar
/home/hduser/workspace/PM1324/libs/android-support-v4.jar
/home/hduser/workspace/PM1324/libs/smack-core-4.1.4.jar
plus
/home/hduser/eclipse-android-sdks/platforms/android-23/android.jar
after including these libraries and importing various classes, there are no Compile time errors in the program :-)
However when I am executing the program in the Simulator, the program crashes (stops working) immediately on startup. Even the "Hello World" string does not show up.
The relevent part of the log shows the following:
10-12 16:41:42.860: E/AndroidRuntime(893): FATAL EXCEPTION: main
10-12 16:41:42.860: E/AndroidRuntime(893): Process: com.yantrajaal.pm1324, PID: 893
10-12 16:41:42.860: E/AndroidRuntime(893): java.lang.NoClassDefFoundError: org.jxmpp.util.XmppStringUtils
10-12 16:41:42.860: E/AndroidRuntime(893): at org.jivesoftware.smack.provider.ProviderManager.getKey(ProviderManager.java:314)
10-12 16:41:42.860: E/AndroidRuntime(893): at org.jivesoftware.smack.provider.ProviderManager.addStreamFeatureProvider(ProviderManager.java:304)
10-12 16:41:42.860: E/AndroidRuntime(893): at org.jivesoftware.smack.provider.ProviderManager.addLoader(ProviderManager.java:140)
10-12 16:41:42.860: E/AndroidRuntime(893): at org.jivesoftware.smack.initializer.UrlInitializer.initialize(UrlInitializer.java:54)
10-12 16:41:42.860: E/AndroidRuntime(893): at org.jivesoftware.smack.SmackInitialization.loadSmackClass(SmackInitialization.java:232)
10-12 16:41:42.860: E/AndroidRuntime(893): at org.jivesoftware.smack.SmackInitialization.parseClassesToLoad(SmackInitialization.java:193)
10-12 16:41:42.860: E/AndroidRuntime(893): at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:163)
10-12 16:41:42.860: E/AndroidRuntime(893): at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:148)
10-12 16:41:42.860: E/AndroidRuntime(893): at org.jivesoftware.smack.SmackInitialization.<clinit>(SmackInitialization.java:116)
10-12 16:41:42.860: E/AndroidRuntime(893): at org.jivesoftware.smack.SmackConfiguration.getVersion(SmackConfiguration.java:96)
10-12 16:41:42.860: E/AndroidRuntime(893): at org.jivesoftware.smack.ConnectionConfiguration.<clinit>(ConnectionConfiguration.java:38)
10-12 16:41:42.860: E/AndroidRuntime(893): at com.yantrajaal.pm1324.MainActivity.onCreate(MainActivity.java:22)
10-12 16:41:42.860: E/AndroidRuntime(893): at android.app.Activity.performCreate(Activity.java:5231)
10-12 16:41:42.860: E/AndroidRuntime(893): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-12 16:41:42.860: E/AndroidRuntime(893): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-12 16:41:42.860: E/AndroidRuntime(893): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-12 16:41:42.860: E/AndroidRuntime(893): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-12 16:41:42.860: E/AndroidRuntime(893): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-12 16:41:42.860: E/AndroidRuntime(893): at android.os.Handler.dispatchMessage(Handler.java:102)
10-12 16:41:42.860: E/AndroidRuntime(893): at android.os.Looper.loop(Looper.java:136)
10-12 16:41:42.860: E/AndroidRuntime(893): at android.app.ActivityThread.main(ActivityThread.java:5001)
10-12 16:41:42.860: E/AndroidRuntime(893): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 16:41:42.860: E/AndroidRuntime(893): at java.lang.reflect.Method.invoke(Method.java:515)
10-12 16:41:42.860: E/AndroidRuntime(893): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-12 16:41:42.860: E/AndroidRuntime(893): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-12 16:41:42.860: E/AndroidRuntime(893): at dalvik.system.NativeStart.main(Native Method)
I would be grateful if someone can please guide me on what am I doing wrong and how I could fix the same.
Thank you in advance for any suggestions, guidance provided.
NEW ISSUE / PROBLEM
The FATAL EXCEPTION and the NoClassDefFoundError has been addressed [ see second answer below ] but now I am getting another problem with the XMPP calls. [ I did not want to create another question on the same problem, so I am continuing here ]
Now the program executes, does not abort but fails to connect to the XMPP server and the log shows the following warning.
10-13 08:48:44.065: W/System.err(1049): org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: 'adastra.re:5222' failed because android.os.NetworkOnMainThreadException
10-13 08:48:44.085: W/System.err(1049): at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:596)
10-13 08:48:44.085: W/System.err(1049): at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:830)
10-13 08:48:44.085: W/System.err(1049): at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:360)
10-13 08:48:44.095: W/System.err(1049): at com.example.pm707.MainActivity.onCreate(MainActivity.java:32)
10-13 08:48:44.095: W/System.err(1049): at android.app.Activity.performCreate(Activity.java:5231)
10-13 08:48:44.095: W/System.err(1049): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-13 08:48:44.095: W/System.err(1049): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-13 08:48:44.095: W/System.err(1049): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-13 08:48:44.095: W/System.err(1049): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-13 08:48:44.095: W/System.err(1049): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-13 08:48:44.095: W/System.err(1049): at android.os.Handler.dispatchMessage(Handler.java:102)
10-13 08:48:44.095: W/System.err(1049): at android.os.Looper.loop(Looper.java:136)
10-13 08:48:44.095: W/System.err(1049): at android.app.ActivityThread.main(ActivityThread.java:5001)
10-13 08:48:44.095: W/System.err(1049): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 08:48:44.095: W/System.err(1049): at java.lang.reflect.Method.invoke(Method.java:515)
10-13 08:48:44.095: W/System.err(1049): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-13 08:48:44.095: W/System.err(1049): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-13 08:48:44.095: W/System.err(1049): at dalvik.system.NativeStart.main(Native Method)
10-13 08:48:44.095: W/System.err(1049): org.jivesoftware.smack.SmackException$NotConnectedException: Client is not, or no longer, connected
10-13 08:48:44.155: W/System.err(1049): at org.jivesoftware.smack.tcp.XMPPTCPConnection.throwNotConnectedExceptionIfAppropriate(XMPPTCPConnection.java:334)
10-13 08:48:44.155: W/System.err(1049): at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:447)
10-13 08:48:44.165: W/System.err(1049): at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:410)
10-13 08:48:44.165: W/System.err(1049): at com.example.pm707.MainActivity.onCreate(MainActivity.java:39)
10-13 08:48:44.165: W/System.err(1049): at android.app.Activity.performCreate(Activity.java:5231)
10-13 08:48:44.165: W/System.err(1049): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-13 08:48:44.165: W/System.err(1049): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-13 08:48:44.165: W/System.err(1049): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-13 08:48:44.175: W/System.err(1049): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-13 08:48:44.175: W/System.err(1049): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-13 08:48:44.185: W/System.err(1049): at android.os.Handler.dispatchMessage(Handler.java:102)
10-13 08:48:44.185: W/System.err(1049): at android.os.Looper.loop(Looper.java:136)
10-13 08:48:44.185: W/System.err(1049): at android.app.ActivityThread.main(ActivityThread.java:5001)
10-13 08:48:44.185: W/System.err(1049): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 08:48:44.185: W/System.err(1049): at java.lang.reflect.Method.invoke(Method.java:515)
10-13 08:48:44.185: W/System.err(1049): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-13 08:48:44.185: W/System.err(1049): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-13 08:48:44.185: W/System.err(1049): at dalvik.system.NativeStart.main(Native Method)
10-13 08:48:44.355: D/(1049): HostConnection::get() New Host Connection established 0xb8104c50, tid 1049
10-13 08:48:44.415: W/EGL_emulation(1049): eglSurfaceAttrib not implemented
Would be grateful for any ideas for resolving this