package com.example.chat; //client for a server
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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private Button b1;
private EditText t1,t2;
public StringBuffer str=new StringBuffer();
public WebSocketClient mWebSocketClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
connectWebSocket();
} catch(Exception e){
e.printStackTrace();
}
runWebSocket();
}
public void runWebSocket(){
b1=(Button)findViewById(R.id.button1);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText)findViewById(R.id.editText2);
str.insert(0,"");
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendNewMessage(v);
}
});
}
public void connectWebSocket() {
URI uri;
try {
uri = new URI("ws://192.168.10.77:8080/Chat/EchoAnnotation");
} catch (URISyntaxException e) {
e.printStackTrace();
return;
}
mWebSocketClient = new WebSocketClient(uri) {
@Override
public void onOpen(ServerHandshake serverHandshake) {
Log.i("Websocket", "Opened");
mWebSocketClient.send("hello server");
str.append("hello server \n");
t2.setText(str);
}
@Override
public void onMessage(String s) {
final String message = s;
runOnUiThread(new Runnable() {
@Override
public void run() {
str.append("Server says: " + message+ "\n");
t2.setText(str);
}
});
}
@Override
public void onClose(int i, String s, boolean b) {
Log.i("Websocket", "Closed " + s);
str.append("WebSocket is closed");
t2.setText(str);
}
@Override
public void onError(Exception e) {
Log.i("Websocket", "Error " + e.getMessage());
str.append("there is some error in the WebSocket");
t2.setText(str);
}
};
mWebSocketClient.connect();
}
public void sendNewMessage(View view){
str.append("Client says: "+t1.getText().toString()+"\n");
mWebSocketClient.send(t1.getText().toString());
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
logcat的:
06-11 05:08:01.601: W/dalvikvm(1045): Unable to resolve superclass of Lcom/example/chat/MainActivity$2; (1380) 06-11 05:08:01.601: W/dalvikvm(1045): Link of class 'Lcom/example/chat/MainActivity$2;' failed 06-11 05:08:01.601: E/dalvikvm(1045): Could not find class 'com.example.chat.MainActivity$2', referenced from method com.example.chat.MainActivity.connectWebSocket 06-11 05:08:01.601: W/dalvikvm(1045): VFY: unable to resolve new-instance 1262 (Lcom/example/chat/MainActivity$2;) in Lcom/example/chat/MainActivity; 06-11 05:08:01.601: D/dalvikvm(1045): VFY: replacing opcode 0x22 at 0x0007 06-11 05:08:01.641: I/dalvikvm(1045): Could not find method org.java_websocket.client.WebSocketClient.send, referenced from method com.example.chat.MainActivity.sendNewMessage 06-11 05:08:01.641: W/dalvikvm(1045): VFY: unable to resolve virtual method 9762: Lorg/java_websocket/client/WebSocketClient;.send (Ljava/lang/String;)V 06-11 05:08:01.641: D/dalvikvm(1045): VFY: replacing opcode 0x6e at 0x0030 06-11 05:08:01.641: W/dalvikvm(1045): Unable to resolve superclass of Lcom/example/chat/MainActivity$2; (1380) 06-11 05:08:01.651: W/dalvikvm(1045): Link of class 'Lcom/example/chat/MainActivity$2;' failed 06-11 05:08:01.661: D/dalvikvm(1045): DexOpt: unable to opt direct call 0x24e7 at 0x09 in Lcom/example/chat/MainActivity;.connectWebSocket 06-11 05:08:02.221: D/dalvikvm(1045): GC_FOR_ALLOC freed 104K, 5% free 3261K/3432K, paused 38ms, total 40ms 06-11 05:08:02.251: D/AndroidRuntime(1045): Shutting down VM 06-11 05:08:02.251: W/dalvikvm(1045): threadid=1: thread exiting with uncaught exception (group=0xb1a83ba8) 06-11 05:08:02.251: E/AndroidRuntime(1045): FATAL EXCEPTION: main 06-11 05:08:02.251: E/AndroidRuntime(1045): Process: com.example.chat, PID: 1045 06-11 05:08:02.251: E/AndroidRuntime(1045): java.lang.NoClassDefFoundError: com.example.chat.MainActivity$2 06-11 05:08:02.251: E/AndroidRuntime(1045): at com.example.chat.MainActivity.connectWebSocket(MainActivity.java:67) 06-11 05:08:02.251: E/AndroidRuntime(1045): at com.example.chat.MainActivity.onCreate(MainActivity.java:35) 06-11 05:08:02.251: E/AndroidRuntime(1045): at android.app.Activity.performCreate(Activity.java:5231) 06-11 05:08:02.251: E/AndroidRuntime(1045): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-11 05:08:02.251: E/AndroidRuntime(1045): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 06-11 05:08:02.251: E/AndroidRuntime(1045): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 06-11 05:08:02.251: E/AndroidRuntime(1045): at android.app.ActivityThread.access$800(ActivityThread.java:135) 06-11 05:08:02.251: E/AndroidRuntime(1045): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-11 05:08:02.251: E/AndroidRuntime(1045): at android.os.Handler.dispatchMessage(Handler.java:102) 06-11 05:08:02.251: E/AndroidRuntime(1045): at android.os.Looper.loop(Looper.java:136) 06-11 05:08:02.251: E/AndroidRuntime(1045): at android.app.ActivityThread.main(ActivityThread.java:5001) 06-11 05:08:02.251: E/AndroidRuntime(1045): at java.lang.reflect.Method.invokeNative(Native Method) 06-11 05:08:02.251: E/AndroidRuntime(1045): at java.lang.reflect.Method.invoke(Method.java:515) 06-11 05:08:02.251: E/AndroidRuntime(1045): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 06-11 05:08:02.251: E/AndroidRuntime(1045): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 06-11 05:08:02.251: E/AndroidRuntime(1045): at dalvik.system.NativeStart.main(Native Method) 06-11 05:08:07.281: I/Process(1045): Sending signal. PID: 1045 SIG: 9
答案 0 :(得分:0)
似乎org.java_websocket.client.WebSocketClient
的库不可用:
logcat消息
无法解析Lcom / example / chat / MainActivity $ 2;
的超类
和
java.lang.NoClassDefFoundError:com.example.chat.MainActivity $ 2
表示您在此段代码中创建的匿名类
mWebSocketClient = new WebSocketClient(uri) {
...
}
无法创建,因为找不到WebSocketClient
的类定义。
某些答案表明您应该检查构建路径设置: