好吧,Android 4.0 不允许在 UI 线程上联网。所以我做了一个新线程。
public class NetThread extends Thread {
BukkitController bc;
Socket sock;
ObjectOutputStream outstream;
ObjectInputStream instream;
boolean loaded=false;
String ServerIP="192.168.123.103";
String id="ssw";
String pw="ssw";
ArrayList<String> plist=new ArrayList<String>();
public void run(){
try {
sock=new Socket(ServerIP,3560);
outstream=new ObjectOutputStream(sock.getOutputStream());
instream=new ObjectInputStream(sock.getInputStream());
//LOGIN
outstream.writeObject("SIGN "+id+" "+pw);
} catch (Exception e){}
Object tmp;
try{
outstream.writeObject("PLIST");
plist=(ArrayList<String>) instream.readObject();
} catch ( Exception e){
}
loaded=true;
while(true){}
}
public void Write(String msg){
try {
outstream.writeObject(msg);
} catch ( Exception e){
}
}
}
在 UI 线程中,我创建了对象:
m_nt=new NetThread();
m_nt.start();
用 LogCat 测试后,我发现源停止在:
sock=new Socket(ServerIP,3560);
我该如何解决这个问题?我没有错误,只是冻结在那里。