java-read()和readObject()的问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-read()和readObject()的问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4383字,纯文字阅读大概需要7分钟。
内容图文
我尝试开发一种纸牌游戏,通过蓝牙玩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﹕ disconnected
java.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()的问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。