我在 [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)
任何想法为什么我在应用程序加载时遇到致命异常?