- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当我的代码卡在 objectinputstream readObject() 函数上时,我想在连接建立后共享包含(字符串名称、地址、标题、...和字节 [] 图片)的 Stuff 类型的对象。没有流式传输发生。任何人都可以找出我做错事的地方。
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);
}
}
}`
最佳答案
当你创建一个 ObjectOutputStream
时,你必须确保套接字另一端的 ObjectInputStream
也被适本地创建,因为 outputStream 总是向 inputStream 发送一个确认数据包并阻塞直到它得到答案。
关于android - 程序卡在 ObjectInputStream readObject 方法上。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9148945/
我在学习 Java IO 的基础知识时发现,为了在类中使用自定义 writeObject 函数,必须将其声明为私有(private)。 private void writeObject(ObjectO
通常,当您反序列化一个类时,您必须为其创建一个单独的实例: try (ObjectInputStream objectInputStream = new ObjectInputStream(new F
这是我的第一个问题,因为到目前为止我总是能找到我需要的东西,但这次一些相对基本的东西让我发疯。所以我有这个类(class): public class QImage { protected Strin
只是为了说明我的问题的背景;我正在编写一个客户端服务器应用程序。当客户端最初连接时,服务器访问数据库,并通过在 for 循环中将对象写入对象输出流,将特定表中的所有项目发送到客户端。 问题是,客户端仅
我正在开发一个 UI,它从 Zookeeper 读取序列化对象,反序列化它,然后将其转换为 JSON。由于某种原因,我无法反序列化 MQTopic 对象。但我可以对其他对象执行相同的操作。 这是将 b
我有一个使用泛型和对象序列化的简单服务器。 (T是输入格式,U是输出格式)。仅处理输入的简化版本如下所示: public class Server implements Runnable {
我正在编写一个服务器/客户端程序。客户端将“请求”(这是为此目的而设计的对象)发送到服务器,服务器使用 ObjectInputStream 对其进行解码。所有“Request”对象都属于同一类,只是数
我正在尝试创建一个实时消息应用程序。但是,当尝试从 ObjectInputStream 读取对象时,代码无缘无故地挂起。没有抛出异常。 try { System.out.println("Tr
我正在尝试测试自定义序列化,但未调用 readObject() 方法。尽管我可以调用 writeObject 方法。我们可以重载readObject方法()吗? public class Te
在我必须做的作业中,我必须编写一个学生对象,其中包含姓名、社会安全号码、通过的类(class)数量以及每门类(class)的成绩。我创建了一个 gpa 方法来计算 gpa,并用我自己的方法重写了 wr
我已将对象保存到数组列表并将文件另存为 .dat,但当我想读取对象时,它只显示 null 而不是值。 类 public class Person implements Serializable {
我刚刚开始学习序列化并尝试实现它。我有一个服务器、一个客户端和一个学生类。 服务器创建我的学生类(class)的初始实例。然后客户端连接到服务器并篡改与学生相关的属性,即提高 GPA。 出于某种原因,
我想从文件中读取不同类型的对象到 ArrayList ,它们都是扩展类 Advertising 的类的实例。我正在尝试使用以下代码: ArrayList ads = new ArrayList();
我们正在尝试使用简单的嵌套对象反序列化一个相当大的对象。这通常需要大约 5 - 10 毫秒。然而,最近我们在这次通话期间遇到了高达 3000 毫秒的随机延迟。我可以一遍又一遍地运行请求,并从调用中获得
我想使用ObjectInputStream从文件中读取对象。下面是 readObject 方法的内部结构: public void readObject(ObjectInputStream inbos
当我运行下面的代码时,objIn.readObject();抛出异常: classNotFoundException project.Edge project.Edge 是我尝试从文件中读取的对象。
我在 Android 应用程序中使用 ObjectOutputStream 和 ObjectInputStream 保存和读取保存在文件中的对象时遇到一些问题我有两个类(class)“锻炼”和“锻炼”
似乎是一个非常简单的问题,但我在谷歌上找不到直接答案。 我在两个进程之间通过 UDP 有一个简单的 IPC channel 。每条消息都包装在 header 类中(例如数据驱动)。 例如。 enum
我的应用程序传输 Twitter 数据并将其写入文件。 while(true){ Status status = queue.poll(); if (status ==
我在加载保存文件时遇到问题,它在 2 个 int 变量的 .readObject() 中显示“找不到符号”错误。我正确声明了变量,并且这两个变量都是全局变量。我不知道为什么。我试图寻找答案,所以我最终
我是一名优秀的程序员,十分优秀!