java-read()和readObject()的问题

java-read()和readObject()的问题,第1张

概述我尝试开发一种纸牌游戏,通过蓝牙玩1vs1.我已经连接了设备,但现在遇到了一个问题:我想发送对象发送蓝牙.如果仅创建对象,则工作,如果仅字符串,则工作.但是,如果我尝试同时制作两者,则会遇到问题./***Thisthreadrunsduringaconnectionwitharemotedevice.*Ithandle

我尝试开发一种纸牌游戏,通过蓝牙玩1vs1.我已经连接了设备,但现在遇到了一个问题:我想发送对象发送蓝牙.
如果仅创建对象,则工作,如果仅字符串,则工作.
但是,如果我尝试同时制作两者,则会遇到问题.

/** * This thread runs during a connection with a remote device. * It handles all incoming and outgoing transmissions. */private class ConnectedThread extends Thread {    private final BluetoothSocket mmSocket;    private final inputStream mmInStream;    private final OutputStream mmOutStream;    // for Objects    private final ObjectinputStream mObjectInStream;    private final ObjectOutputStream mObjectOutStream;    public ConnectedThread(BluetoothSocket socket) {        if (D) Log.d(TAG, "create ConnectedThread");        mmSocket = socket;        inputStream tmpIn = null;        OutputStream tmpOut = null;        ObjectinputStream tmpObjIn = null;        ObjectOutputStream tmpObjOut = null;        // Get the BluetoothSocket input and output streams        try {            tmpIn = socket.getinputStream();            tmpOut = socket.getoutputStream();            tmpObjOut = new ObjectOutputStream(socket.getoutputStream());            tmpObjOut.flush();            tmpObjIn = new ObjectinputStream(socket.getinputStream());        } catch (IOException e) {            Log.e(TAG, "temp sockets not created", e);        }        mmInStream = tmpIn;        mmOutStream = tmpOut;        mObjectOutStream = tmpObjOut;        mObjectInStream = tmpObjIn;    }    public voID run() {        if (D) Log.i(TAG, "BEGIN mConnectedThread");        byte[] buffer = new byte[1024];        int bytes;        // Keep Listening to the inputStream while connected        while (true) {                try {                    // Read from the inputStream                    bytes = mmInStream.read(buffer);                    // Send the obtained bytes to the UI Activity                    mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)                            .sendToTarget();                } catch (IOException e) {                    Log.e(TAG, "disconnected", e);                    connectionLost();                    // Start the service over to restart Listening mode                    BluetoothService.this.start();                    break;                }                try {                    // Send the obtained Object to the UI Activity                    mHandler.obtainMessage(Constants.MESSAGE_READ_OBJECT, -1, -1, mObjectInStream.readobject())                            .sendToTarget();                } catch (IOException e) {                    Log.e(TAG, "disconnected", e);                    connectionLost();                    // Start the service over to restart Listening mode                    BluetoothService.this.start();                    break;                } catch (ClassNotFoundException cn) {                    Log.e(TAG, "Class not found", cn);                }            }    }    /**     * Write to the connected OutStream.     *     * @param buffer The bytes to write     */    public voID writeString(byte[] buffer) {        try {            mmOutStream.write(buffer);            // Share the sent message back to the UI Activity            mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)                    .sendToTarget();        } catch (IOException e) {            Log.e(TAG, "Exception during write", e);        }    }    /**     * Write an Object (Serializable) to the connected OutStream.     *     * @param object The object to write     */    public voID writeObject(Object object) {        try {            mObjectOutStream.writeObject(object);            // Share the sent message back to the UI Activity            // Todo hIEr unterscheIDen zwischen Player und UnoKarte?            mHandler.obtainMessage(Constants.MESSAGE_WRITE_OBJECT, -1, -1, object)                    .sendToTarget();        } 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);        }    }}

错误:

06-21 14:18:44.580  10941-11034/? E/BluetoothService﹕ disconnectedjava.io.StreamCorruptedException: Wrong format: 0        at java.io.ObjectinputStream.corruptStream(ObjectinputStream.java:830)        at java.io.ObjectinputStream.readNonPrimitiveContent(ObjectinputStream.java:943)        at java.io.ObjectinputStream.readobject(ObjectinputStream.java:2262)        at java.io.ObjectinputStream.readobject(ObjectinputStream.java:2217)        at com.example.thm_wip1.uno.BluetoothService$ConnectedThread.run(BluetoothService.java:550)

第550行是:mHandler.obtainMessage(Constants.MESSAGE_READ_OBJECT,-1,-1,mObjectInStream.readobject())

在While(true)中,我有两个try-catch,第一个尝试读取字符串,第二个尝试读取Object.如何在运行方法中区分字符串和对象?
我是套接字,inputStream,outputStream的新手.

如果您需要更多详细信息,我将编辑我的问题.

解决方法:

当设备的内部一致性检查中的标头读取不一致时,会发生StreamCorruptedException,在这种情况下,一致性检查失败是因为您尝试使用多个输出流和输入流,第一个在tmpout和tmpin中定义,然后尝试创建ObjectOutputStreams和ObjectinputStreams分别来自不同的输出流和输入流.这些会导致标头不一致.

应该解决此问题的正确代码如下

 try {        tmpIn = socket.getinputStream();        tmpOut = socket.getoutputStream();        tmpObjOut = new ObjectOutputStream(tmpOut);        tmpObjOut.flush();        tmpObjIn = new ObjectinputStream(tmpIn);    } catch (IOException e) {        Log.e(TAG, "temp sockets not created", e);    }
总结

以上是内存溢出为你收集整理的java-read()和readObject()的问题全部内容,希望文章能够帮你解决java-read()和readObject()的问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1093402.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存