程序挂在ObjectInputStream中的readObject方法。 [英] program hangs on ObjectInputStream readObject method .

查看:376
本文介绍了程序挂在ObjectInputStream中的readObject方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分享当连接的是,我的code挂在ObjectInputStream中的readObject()函数后成立的类型的东西,其中包含的对象(字符串名称,地址,职务,......和字节[]图片)。不会发生流。任何人都可以请找出其中我做错了事情。

 私有类ConnectedThread继承Thread {
        私人最终的BluetoothSocket mmSocket;
        私人最终的InputStream mmInStream;
        私人最终的OutputStream mmOutStream;
        私人的ObjectOutputStream OOS = NULL;
        私人ObjectInputStream的OIS = NULL;        公共ConnectedThread(插座的BluetoothSocket){
            Log.d(TAG,创造ConnectedThread);
            mmSocket =插座;
            Log.d(TAG,创造);
            InputStream的tmpIn = NULL;
            Log.d(TAG,打造B);
            OutputStream的tmpOut = NULL;            //获取的BluetoothSocket输入和输出流
            尝试{
                Log.d(之前的InputStream连接线构造,);
                tmpIn = socket.getInputStream();
                Log.d(连接线构造的InputStream
                        tmpIn.toString());
                tmpOut = socket.getOutputStream();
                Log.d(连接线构造的OutputStream
                        tmpOut.toString());
            }赶上(IOException异常五){
                Log.e(TAG,临时插座没有创造,E);
            }            mmInStream = tmpIn;
            mmOutStream = tmpOut;            最终的BufferedOutputStream蟾蜍=新的BufferedOutputStream(
                    mmOutStream);
            最终的BufferedInputStream bufi =新的BufferedInputStream(mmInStream);            Log.d(TAG,试图创建OOS);            // ********* **********的ObjectOutputStream            尝试{
                OOS =新的ObjectOutputStream(蟾蜍);            }赶上(StreamCorruptedException E){
                Log.d(TAG,中招损坏流异常);
                Log.w(TAG,E);            }赶上(IOException异常五){
                Log.d(TAG,捕获到IOException);
                Log.w(TAG,E);
            }            // ********* ********** ObjectInputStream的            螺纹s =新的Thread(){
                公共无效的run(){
                    Log.d(TAG,试图创建OIS);
                    尝试{
                        OIS =新的ObjectInputStream(bufi);
                    }赶上(StreamCorruptedException E){
                        // TODO自动生成catch块
                        e.printStackTrace();
                    }赶上(IOException异常五){
                        // TODO自动生成catch块
                        e.printStackTrace();
                    }
                    Log.d(TAG,完成了OIS);
                    如果(OIS == NULL){
                        Log.d(TAG,OIS为空);
                    }
                }            };
            s.start();        }        公共无效的run(){
            Log.i(TAG,BEGIN mConnectedThread);

我的code挂在这一点上,永远向前。

 `//继续听InputStream的同时连接
        而(真){
            尝试{
                Log.d(连接线运行,开始时);                尝试{                    东西obj_rcv =(东西)ois.readObject();
                    Log.d(BTS,RCV对象+ obj_rcv.getName());                    消息MSG 2 = mHandler
                            .obtainMessage(RemoteBusinessCard.MESSAGE_READ);
                    束束=新包();
                    bundle.putSerializable(人,obj_rcv);
                    msg2.setData(包);
                    mHandler.sendMessage(MSG 2);                }赶上(ClassNotFoundException的E1){
                    // TODO自动生成catch块
                    e1.printStackTrace();
                }
            }赶上(IOException异常五){
                Log.e(TAG,断开,E);
                失去连接();
                打破;
            }
        }
    }    / **
     *写到所连接的OutStream。
     *
     * @参数缓冲区
     *将要写入的字节
     * /
    公共无效写入(东西对象){
        尝试{
            Log.d(BTS,里面写前的+ object.getName());
            oos.writeObject(对象);
            Log.d(BTS,内写入后+ object.getName());
            oos.flush();
            oos.close();        }赶上(IOException异常五){
            Log.e(TAG,写在异常,E);
        }
    }    公共无效取消(){
        尝试{
            mmSocket.close();
        }赶上(IOException异常五){
            Log.e(无法连接插座的close()标签,电子);
        }
    }
}`


解决方案

当你创建一个的ObjectOutputStream 您必须确保的ObjectInputStream 在插座的另一端也适当地创建,因为OutputStream的总发送确认包的InputStream和块,直到它得到一个答案。

I want to share object of type Stuff which contains ( String name,address,title, ... and byte[] picture ) when connection is established after that my code hangs on objectinputstream readObject() function . No streaming occurs. Can anyone please figure out where i am doing wrong thing.

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");

My code hang at this point , never move forward .

        `// 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);
        }
    }
}`

解决方案

When you create an ObjectOutputStream you must ensure that the ObjectInputStream at the other end of the socket is also appropriately created because the outputStream always sends a confirmation packet to the inputStream and blocks until it gets an answer.

这篇关于程序挂在ObjectInputStream中的readObject方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆