0

我在 [address]:1337 有一个 socket.io 服务器。我正在使用以下 java socket-io 客户端:

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

我的 MainActivity.java 文件包含:

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;
}

}

这是我的日志:

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)

任何想法为什么我在应用程序加载时遇到致命异常?

4

2 回答 2

1

您必须将 socket.io-java-client 构建为 .jar 文件(或从此处下载http://www.mediafire.com/download/ifcm4p8fs035baa/socketio.jar)并将其添加到项目的 libs 文件夹中。

于 2013-09-27T04:44:13.483 回答
1

NoClassDefFoundError表示您的项目没有附加所需的库

尝试将所有包含的库从 gottox 添加到您的文件夹库中,将所有需要的内容复制粘贴到项目文件夹中的文件夹库中

需要在这里:

  1. WebSocket.jar
  2. json-org.jar
于 2013-07-12T09:21:24.553 回答