我用谷歌搜索了它和 StackoverFlow,但我可以使用的信息不多。
而且大多数答案都建议使用线程,不要在主线程中打开套接字。
反正我的代码是这样的
public class Client implements Runnable {
private Socket socket;
private static ObjectOutputStream oos;
public Client() {
mPauseLock = new Object();
mPaused = false;
mFinished = false;
try {
socket = new Socket("168.131.148.50", 5001);
pw = new PrintWriter(socket.getOutputStream(), true);
oos = new ObjectOutputStream(socket.getOutputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
for (int i = 0; i < MsgQueue.getSize(); i++) {
try {
oos.writeObject(MsgQueue.get(i));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
并在MainActivity
new Thread(new Client()).start();
但我仍然得到NetworkOnMainThreadExcpetion ..
有人可以告诉我解决方案吗?