尝试使用Socket.io连接Android以接收致命异常

时间:2013-05-25 22:48:06

标签: android socket.io

我在[地址]: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;
}

}

这是我的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)

当应用加载时,为什么我会遇到致命异常的任何想法?

2 个答案:

答案 0 :(得分:1)

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

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

所需要的是:

  1. WebSocket.jar
  2. JS​​ON-org.jar

答案 1 :(得分:1)

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