我的Android(3.2)Phonegap插件中存在一个大问题:
我创建了一个插件,用于打开TCP套接字,发送命令并接收响应,并将其传回JS脚本。
try {
int port = args.getInt(0);
String host = args.getString(1);
String message = args.getString(2);
boolean is_pure_text = args.getBoolean(3);
static Socket sock;
String reply;
try {
if(sock==null || sock.isClosed()){
sock = new Socket(host, port);
sock.setSoTimeout(1000);
Log.d("TCPSockets", "Socket created");
}
DataOutputStream dataOutputStream = new DataOutputStream(sock.getOutputStream());;
DataInputStream dataInputStream = new DataInputStream(sock.getInputStream());
Log.d("TCPSockets", "Created reader/writer");
dataOutputStream.writeUTF(message);
Log.d("TCPSockets", "Sent message");
reply="";
int i;
int tmp;
byte[] b = new byte[40];
for(i=0;i<39;i++){
tmp=dataInputStream.readUnsignedByte();
Log.d("TCPSockets", "READED BYTE: "+tmp);
b[i]=(byte)tmp;
}
byte[] buffer = new byte[i];
System.arraycopy (b, 0, buffer, 0, i);
reply = new String(buffer, "US-ASCII");
callbackContext.success(reply);
Log.d("TCPSockets", "Received reply: " + reply);
} catch (UnknownHostException e) {
Log.d("TCPSockets", "Unknown Host "+host+":"+port);
callbackContext.error("Unknown Host "+host+":"+port);
} catch (IOException e) {
Log.d("TCPSockets", e.getMessage());
callbackContext.error(e.getMessage());
} catch(Exception e){
Log.d("TCPSockets", "Errore Generico "+e.getMessage());
callbackContext.error("Errore Generico "+e.getMessage());
}
我跑的时候......
02-04 18:12:15.198: D/TCPSockets(10875): Created reader/writer
02-04 18:12:15.198: D/TCPSockets(10875): Sent message
02-04 18:12:15.218: D/TCPSockets(10875): 6
02-04 18:12:16.218: E/dalvikvm(10875): JNI ERROR (app bug): attempt to use stale local reference 0x1
02-04 18:12:16.218: E/dalvikvm(10875): VM aborting
02-04 18:12:16.218: A/libc(10875): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1)
如果我删除了回复的读取,则代码可以正常工作。请帮帮我:(谢谢