1

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

如果我删除对回复的阅读,则代码可以正常工作。请帮助我:(谢谢

4

0 回答 0