0

I have a socket.io server at [address]:1337. I'm using the following java socket-io client:

https://github.com/Gottox/socket.io-java-client

My MainActivity.java file contains:

package com.ponzicoder.sailssocketex;

import java.net.MalformedURLException;

import org.json.JSONException;
import org.json.JSONObject;

import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SocketIO socket;
    try {
        socket = new SocketIO("http://[valid address]:1337/");

        socket.connect(new IOCallback() {

            public void onMessage(JSONObject json, IOAcknowledge ack) {
                try {
                    System.out.println("Server said:" + json.toString(2));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onMessage(String data, IOAcknowledge ack) {
                System.out.println("Server said: " + data);
            }

            public void onError(SocketIOException socketIOException) {
                System.out.println("an Error occured");
                socketIOException.printStackTrace();
            }

            public void onDisconnect() {
                System.out.println("Connection terminated.");
            }

            public void onConnect() {
                System.out.println("Connection established");
            }

            public void on(String event, IOAcknowledge ack, Object... args) {
                System.out.println("Server triggered event '" + event + "'");
            }

        }); 
    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.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;
}

}

Here's my logcat:

05-25 17:07:00.891: E/AndroidRuntime(22236): FATAL EXCEPTION: main
05-25 17:07:00.891: E/AndroidRuntime(22236): java.lang.NoClassDefFoundError:   io.socket.SocketIO
05-25 17:07:00.891: E/AndroidRuntime(22236):    at com.ponzicoder.sailssocketex.MainActivity.onCreate(MainActivity.java:25)
05-25 17:07:00.891: E/AndroidRuntime(22236):    at android.app.Activity.performCreate(Activity.java:5250)
05-25 17:07:00.891: E/AndroidRuntime(22236):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
05-25 17:07:00.891: E/AndroidRuntime(22236):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
05-25 17:07:00.891: E/AndroidRuntime(22236):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
05-25 17:07:00.891: E/AndroidRuntime(22236):    at android.app.ActivityThread.access$700(ActivityThread.java:151)
05-25 17:07:00.891: E/AndroidRuntime(22236):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
05-25 17:07:00.891: E/AndroidRuntime(22236):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 17:07:00.891: E/AndroidRuntime(22236):    at android.os.Looper.loop(Looper.java:137)
05-25 17:07:00.891: E/AndroidRuntime(22236):    at android.app.ActivityThread.main(ActivityThread.java:5293)
05-25 17:07:00.891: E/AndroidRuntime(22236):    at java.lang.reflect.Method.invokeNative(Native Method)
05-25 17:07:00.891: E/AndroidRuntime(22236):    at java.lang.reflect.Method.invoke(Method.java:511)
05-25 17:07:00.891: E/AndroidRuntime(22236):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-25 17:07:00.891: E/AndroidRuntime(22236):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-25 17:07:00.891: E/AndroidRuntime(22236):    at dalvik.system.NativeStart.main(Native Method)

Any ideas why I'm getting a fatal exception, when the app loads?

JohnGalt
  • 2,851
  • 2
  • 21
  • 29
  • I am facing the same issue . – Chris May 25 '13 at 23:01
  • @Chris not sure if this will help you but I found this on the NoClassDefFoundError, this didn't work for me http://stackoverflow.com/questions/8678630/noclassdeffounderror-android – JohnGalt May 25 '13 at 23:16
  • It def. has to do with the manner in which the jar compiles , it works properly in the standalone java component which i wrote and included the same jar. This error occurs only in the android emulator . – Chris May 25 '13 at 23:42
  • Did you ever get to resolve the issue ? – Chris May 26 '13 at 18:55
  • Unfortunately no, it's really frustrating. – JohnGalt May 27 '13 at 03:53
  • I have added a issue in the github and i moved on to this [library](https://github.com/koush/android-websockets) and was able to achieve the same functionality . Hope it helps . – Chris May 27 '13 at 16:42
  • Hi Chris, I gave that library a try but I'm getting the same Fatal Error. I'm curious did you create the three classes and then use the example code for the socket? – JohnGalt May 28 '13 at 02:27

2 Answers2

1

NoClassDefFoundError means there's no required library attached to your project

try to add all contains library from gottox to your folder libs, copy paste all required to folder libs at your project folders

a required is here :

  1. WebSocket.jar
  2. json-org.jar
fhamicode
  • 2,498
  • 1
  • 12
  • 7
1

You must build socket.io-java-client to .jar file (or download from here http://www.mediafire.com/download/ifcm4p8fs035baa/socketio.jar) and add it into libs folder of your project .

Ba Tới Xì Cơ
  • 482
  • 4
  • 16