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

我尝试开发一种纸牌游戏,通过蓝牙玩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的新手.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值