【问题标题】:program hangs on ObjectInputStream readObject method .程序挂在 ObjectInputStream 的 readObject 方法上。
【发布时间】:2012-02-27 06:27:07
【问题描述】:

我想在建立连接后共享包含(字符串名称、地址、标题、...和字节[]图片)的 Stuff 类型的对象,之后我的代码挂在 objectinputstream readObject() 函数上。不发生流式传输。谁能弄清楚我在哪里做错了。

private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;
        private ObjectOutputStream oos = null;
        private ObjectInputStream ois = null;

        public ConnectedThread(BluetoothSocket socket) {
            Log.d(TAG, "create ConnectedThread");
            mmSocket = socket;
            Log.d(TAG, "create a");
            InputStream tmpIn = null;
            Log.d(TAG, "create b");
            OutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                Log.d("connected thread constructor  before inputstream", "");
                tmpIn = socket.getInputStream();
                Log.d("connected thread constructor  inputstream",
                        tmpIn.toString());
                tmpOut = socket.getOutputStream();
                Log.d("connected thread constructor outputstream",
                        tmpOut.toString());
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;

            final BufferedOutputStream bufo = new BufferedOutputStream(
                    mmOutStream);
            final BufferedInputStream bufi = new BufferedInputStream(mmInStream);

            Log.d(TAG, "attempting to create OOS");

            // ********* ObjectOutputStream **********

            try {
                oos = new ObjectOutputStream(bufo);

            } catch (StreamCorruptedException e) {
                Log.d(TAG, "Caught Corrupted Stream Exception");
                Log.w(TAG, e);

            } catch (IOException e) {
                Log.d(TAG, "Caught IOException");
                Log.w(TAG, e);
            }

            // ********* ObjectInputStream **********

            Thread s = new Thread() {
                public void run() {
                    Log.d(TAG, "attempting to create OIS");
                    try {
                        ois = new ObjectInputStream(bufi);
                    } catch (StreamCorruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Log.d(TAG, "completed OIS");
                    if (ois == null) {
                        Log.d(TAG, "OIS is null");
                    }
                }

            };
            s.start();

        }

        public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");

我的代码此时挂起,永远不会前进。

        `// Keep listening to the InputStream while connected
        while (true) { 
            try {
                Log.d("Connected thread run ", "start while");

                try {

                    Stuff obj_rcv = (Stuff) ois.readObject();
                    Log.d("BTS", "rcv object " + obj_rcv.getName());

                    Message msg2 = mHandler
                            .obtainMessage(RemoteBusinessCard.MESSAGE_READ);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("person", obj_rcv);
                    msg2.setData(bundle);
                    mHandler.sendMessage(msg2);

                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                break;
            }
        }
    }

    /**
     * Write to the connected OutStream.
     * 
     * @param buffer
     *            The bytes to write
     */
    public void write(Stuff object) {
        try {
            Log.d("BTS", "inside write before" + object.getName());
            oos.writeObject(object);
            Log.d("BTS", "inside write after" + object.getName());
            oos.flush();
            oos.close();

        } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
        }
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }
}`

【问题讨论】:

    标签: android bluetooth objectoutputstream objectinputstream


    【解决方案1】:

    当您创建ObjectOutputStream 时,您必须确保套接字另一端的ObjectInputStream 也被适当地创建,因为 outputStream 总是向 inputStream 发送一个确认数据包并阻塞,直到它得到答案。

    【讨论】:

    • 如果 ObjectOutputStream 首先在主设备和从设备上执行,那么 ObjectInputStream 将如何接收数据,因为它还没有初始化!!
    • 你必须轮流创建它们。例如客户端上的 InputStream 和服务器上的 OutputStream ,然后是客户端上的 OutputStream 和服务器上的 InputStream 。就像同时在相反的溪流中一样。
    【解决方案2】:

    这不是 java 的工作方式。 您必须为输入和输出流创建两个单独的线程,以便您的逻辑正常工作。

    【讨论】:

      【解决方案3】:
      outStream = new ObjectOutputStream(socket.getOutputStream());
      outStream.flush();
      inStream = new ObjectInputStream(socket.getInputStream());
      

      首先在服务器和客户端创建输出流,刷新和输入流

      【讨论】:

        猜你喜欢
        • 2012-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        相关资源
        最近更新 更多