我正在使用库 Gottox/socket.io-java-client 使用套接字 io 制作 android 应用程序我成功地将数据发送到服务器,但是我在接收来自服务器的响应时遇到问题我收到了这些错误
09-14 01:31:43.441: E/AndroidRuntime(585): FATAL EXCEPTION: main
09-14 01:31:43.441: E/AndroidRuntime(585): java.lang.NullPointerException
09-14 01:31:43.441: E/AndroidRuntime(585): at a.a.a.SocketsActivity$1.onClick(SocketsActivity.java:121)
09-14 01:31:43.441: E/AndroidRuntime(585): at android.view.View.performClick(View.java:2408)
09-14 01:31:43.441: E/AndroidRuntime(585): at android.view.View$PerformClick.run(View.java:8816)
09-14 01:31:43.441: E/AndroidRuntime(585): at android.os.Handler.handleCallback(Handler.java:587)
09-14 01:31:43.441: E/AndroidRuntime(585): at android.os.Handler.dispatchMessage(Handler.java:92)
09-14 01:31:43.441: E/AndroidRuntime(585): at android.os.Looper.loop(Looper.java:123)
09-14 01:31:43.441: E/AndroidRuntime(585): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-14 01:31:43.441: E/AndroidRuntime(585): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 01:31:43.441: E/AndroidRuntime(585): at java.lang.reflect.Method.invoke(Method.java:521)
09-14 01:31:43.441: E/AndroidRuntime(585): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-14 01:31:43.441: E/AndroidRuntime(585): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-14 01:31:43.441: E/AndroidRuntime(585): at dalvik.system.NativeStart.main(Native Method)
09-14 01:31:48.433: I/global(585): Default buffer size used in BufferedOutputStream constructor. It would be better to be explicit if an 8k buffer is required.
09-14 01:31:49.481: I/io.socket(585): > 3:::{"Action": "AuthenticateUser","Parameters":[{"UserName": "a","Password": "a"}]}
09-14 01:31:49.481: I/io.socket(585): < 1::
09-14 01:31:50.681: I/io.socket(585): < 5:::{"name":"message","args":[{"Action":"AuthenticateUser","RequestStatus":"100","RequestStatusMessage":"Invalid UserName/password"}]}
09-14 01:31:50.681: W/System.err(585): a.a.a.SocketIOException: Exception was thrown in on(String, JSONObject[]).
09-14 01:31:50.681: W/System.err(585): Message was: 5:::{"name":"message","args":[{"Action":"AuthenticateUser","RequestStatus":"100","RequestStatusMessage":"Invalid UserName/password"}]}
09-14 01:31:50.691: W/System.err(585): at a.a.a.IOConnection.transportMessage(IOConnection.java:684)
09-14 01:31:50.691: W/System.err(585): at a.a.a.WebsocketTransport.onMessage(WebsocketTransport.java:99)
09-14 01:31:50.691: W/System.err(585): at de.roderick.weberknecht.WebSocketReceiver.run(WebSocketReceiver.java:57)
09-14 01:31:50.691: W/System.err(585): Caused by: java.lang.ClassCastException: a.a.a.IOConnection
09-14 01:31:50.691: W/System.err(585): at a.a.a.IOConnection.findCallback(IOConnection.java:487)
09-14 01:31:50.701: W/System.err(585): at a.a.a.IOConnection.transportMessage(IOConnection.java:679)
09-14 01:31:50.701: W/System.err(585): ... 2 more
这是我的代码
private void SendDataToServer(String username,String password)
{
SocketIO socket=new SocketIO();
try {
socket = new SocketIO("http://41.69.103.53:1333/");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
socket.connect(new IOCallback() {
public void onMessage(JSONObject json, IOAcknowledge ack) {
try {
System.out.println("Server said:" + json.toString(2));
// x=json.toString(2);
// z=json.toString();
} catch (JSONException e) {
e.printStackTrace();
}
}
public void onMessage(String data, IOAcknowledge ack) {
//tv.setText(data);
//System.out.println("Server said: " + data);
// x=data;
}
public void onError(SocketIOException socketIOException) {
//tv.setText(socketIOException.toString());
//System.out.println("an Error occured");
request=0;
socketIOException.printStackTrace();
}
public void onDisconnect() {
// tv.setText("Connection terminated.");
//System.out.println("Connection terminated.");
request=0;
}
public void onConnect() {
// tv.setText("Connection terminated.");
// System.out.println("Connection established");
}
public void on(String event, IOAcknowledge ack, Object... args) {
//tv.setText("Server triggered event '" + event + "'");
// System.out.println("Server triggered event '" + event + "'");
x=event;
}
});
// This line is cached until the connection is establisched.
String request="{\"Action\": \"AuthenticateUser\""+","+
"\"Parameters\":";
String request_2="[{"+"\"UserName\": "+"\""+username_string+"\""+","+
"\"Password\""+": "+"\""+password_string+"\""+"}]}";
String FinalRequest_login=request+request_2;
socket.send(FinalRequest_login);
}
我找到了导致异常的行
public void transportMessage(String text) {
logger.info("< " + text);
IOMessage message;
try {
message = new IOMessage(text);
} catch (Exception e) {
error(new SocketIOException("Garbage from server: " + text, e));
return;
}
resetTimeout();
switch (message.getType()) {
case IOMessage.TYPE_DISCONNECT:
try {
findCallback(message).onDisconnect();
} catch (Exception e) {
error(new SocketIOException(
"Exception was thrown in onDisconnect()", e));
}
break;
case IOMessage.TYPE_EVENT:
try {
JSONObject event = new JSONObject(message.getData());
Object[] argsArray;
if (event.has("args")) {
JSONArray args = event.getJSONArray("args");
argsArray = new Object[args.length()];
for (int i = 0; i < args.length(); i++) {
if (args.isNull(i) == false)
argsArray[i] = args.get(i);
}
} else
argsArray = new Object[0];
String eventName = event.getString("name");
try {
findCallback(message).on(eventName,
remoteAcknowledge(message), argsArray);
} **catch (Exception e) {
error(new SocketIOException(
"Exception was thrown in on(String, JSONObject[]).\n"
+ "Message was: " + message.toString(), e));**
}
这是异常 捕获 (Exception e) { error(new SocketIOException("Exception was throw in on(String, JSONObject[]).\n" + "Message was: " + message.toString(), e));
因为这个功能
public void on(String event, IOAcknowledge ack, Object... args) {
for (SocketIO socket : sockets.values())
socket.getCallback().on(event, ack, args);
}
任何人都可以帮我解决它或帮助我为什么我会得到这个例外?提前谢谢