Java中的Websocket客户端 - NoClassDefFoundError

时间:2015-08-22 12:07:32

标签: java android websocket

我在Eclipse上有Android项目。主要思想是创建java websocket客户端。客户端必须向服务器发送消息并返回一些消息。但是当我在模拟器上运行应用程序时,它会崩溃。在Log中,有一个NoClassDefFoundError。错误在哪里?

我的 main.xml 非常简单:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
       android:id="@+id/messages"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </LinearLayout>

MainActivity.java:

package com.ab.mywebsocketclient;

import java.net.URI;
import java.net.URISyntaxException;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {
private WebSocketClient mWebSocketClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    connectWebSocket();
}


private void connectWebSocket() {
    URI uri;
    try {
      uri = new URI("ws://127.0.0.1:80");
    } 
    catch (URISyntaxException e) {
        e.printStackTrace();
        return;
      }
    mWebSocketClient = new WebSocketClient(uri) {
        @Override
        public void onOpen(ServerHandshake serverHandshake) {
            Log.i("Websocket", "Opened");
            mWebSocketClient.send("123"); 
            }
        @Override
        public void onMessage(String s) {
            final String message = s;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                      TextView msg = (TextView)findViewById(R.id.messages);
                    msg.setText(msg.getText() + "\n" + message);
                }
            });
        }
        @Override
        public void onClose(int i, String s, boolean b) {
            Log.i("Websocket", "Closed " + s);
        }
        @Override
        public void onError(Exception e) {
            Log.i("Websocket", "Error " + e.getMessage());
        }
    };
    mWebSocketClient.connect();
}
}

Manifest.xml 中添加 uses-permission android:name =“android.permission.INTERNET”

这是日志

08-22 10:53:10.974: D/AndroidRuntime(1251): Shutting down VM
08-22 10:53:10.974: D/AndroidRuntime(1251): --------- beginning of crash
08-22 10:53:10.991: E/AndroidRuntime(1251): FATAL EXCEPTION: main
08-22 10:53:10.991: E/AndroidRuntime(1251): Process: com.ab.mywebsocketclient, PID: 1251
08-22 10:53:10.991: E/AndroidRuntime(1251): Process: com.ab.mywebsocketclient, PID: 1251
08-22 10:53:10.991: E/AndroidRuntime(1251): java.lang.NoClassDefFoundError: Failed resolution of: Lorg/java_websocket/client/WebSocketClient;
08-22 10:53:10.991: E/AndroidRuntime(1251):     at com.ab.mywebsocketclient.MainActivity.connectWebSocket(MainActivity.java:27)
08-22 10:53:10.991: E/AndroidRuntime(1251):     at com.ab.mywebsocketclient.MainActivity.onCreate(MainActivity.java:22)
08-22 10:53:10.991: E/AndroidRuntime(1251):     at android.app.Activity.performCreate(Activity.java:5937)
08-22 10:53:10.991: E/AndroidRuntime(1251):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
08-22 10:53:10.991: E/AndroidRuntime(1251):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
08-22 10:53:10.991: E/AndroidRuntime(1251):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
08-22 10:53:10.991: E/AndroidRuntime(1251):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
08-22 10:53:10.991: E/AndroidRuntime(1251):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
08-22 10:53:10.991: E/AndroidRuntime(1251):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-22 10:53:10.991: E/AndroidRuntime(1251):     at android.os.Looper.loop(Looper.java:135)
08-22 10:53:10.991: E/AndroidRuntime(1251):     at android.app.ActivityThread.main(ActivityThread.java:5221)
08-22 10:53:10.991: E/AndroidRuntime(1251):     at java.lang.reflect.Method.invoke(Native Method)
08-22 10:53:10.991: E/AndroidRuntime(1251):     at java.lang.reflect.Method.invoke(Method.java:372)
08-22 10:53:10.991: E/AndroidRuntime(1251):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
08-22 10:53:10.991: E/AndroidRuntime(1251):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
08-22 10:53:10.991: E/AndroidRuntime(1251): Caused by: java.lang.ClassNotFoundException: Didn't find class "org.java_websocket.client.WebSocketClient" on path: DexPathList[[zip file "/data/app/com.ab.mywebsocketclient-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
08-22 10:53:10.991: E/AndroidRuntime(1251):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
08-22 10:53:10.991: E/AndroidRuntime(1251):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
08-22 10:53:10.991: E/AndroidRuntime(1251):     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
08-22 10:53:10.991: E/AndroidRuntime(1251):     ... 15 more
08-22 10:53:10.991: E/AndroidRuntime(1251):     Suppressed: java.lang.ClassNotFoundException: org.java_websocket.client.WebSocketClient
08-22 10:53:10.991: E/AndroidRuntime(1251):         at java.lang.Class.classForName(Native Method)
08-22 10:53:10.991: E/AndroidRuntime(1251):         at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
08-22 10:53:10.991: E/AndroidRuntime(1251):         at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
08-22 10:53:10.991: E/AndroidRuntime(1251):         at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
08-22 10:53:10.991: E/AndroidRuntime(1251):         ... 16 more
08-22 10:53:10.991: E/AndroidRuntime(1251):     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
08-22 10:53:16.681: I/Process(1251): Sending signal. PID: 1251 SIG: 9

2 个答案:

答案 0 :(得分:1)

在Android Studio中,您可以尝试在build.gradle中添加编译行:

compile 'org.java-websocket:Java-WebSocket:1.3.0'

http://mvnrepository.com/artifact/org.java-websocket/Java-WebSocket/1.3.0

找到

答案 1 :(得分:0)

在您的应用程序级别build.gradle文件中添加以下行

 repositories {
mavenCentral()
 }

在依赖项{}中添加此实现

 implementation "org.java-websocket:Java-WebSocket:1.3.0"