gpt4 book ai didi

java - 异步任务中的 getInputStream

转载 作者:行者123 更新时间:2023-12-01 14:44:42 26 4
gpt4 key购买 nike

我有一个 Activity ,它调用异步任务以通过预定义的 InetAddress 接受套接字连接。

异步任务调用另一个异步任务来监听消息。但它卡在获取输入流上

我已经绞尽脑汁好几个小时了,但无法弄清楚它为什么挂起......请帮忙。

public void startSocketListener(InetAddress groupOwnerAddress) {
// TODO Auto-generated method stub
AcceptClientThread accept;
try {
accept = new AcceptClientThread();
accept.execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

public class AcceptClientThread extends AsyncTask<Void, String, String>{

public AcceptClientThread() throws IOException{

}

@Override
protected void onCancelled() {
// TODO Auto-generated method stub
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("CONNECTION ERR", "Could not close serverSocket " + e.toString());
}
super.onCancelled();
}

@Override
protected String doInBackground(Void... params) {

try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
Log.e("CONNECTION ERR","Could not listen on port: " + port);
onCancelled();
}

while (listening){
try {
Log.i("CONNECTION", "AWAITING CONNECTION TO CLIENT");
Socket newSocket = serverSocket.accept();
Log.i("CONNECTION", "CONNECTED TO CLIENT");
ListenerThread lThread = new ListenerThread(newSocket);
lThread.execute("Do it");
Log.i("CONNECTION", "ACCEPTED CLIENT");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
onProgressUpdate("could not accept client");
}

}
Log.i("CONNECTION", "close socket");
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return "table connected";
}

@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
// received data is first element in the String
//Toast.makeText(KitchenActivity.this, values[0], Toast.LENGTH_SHORT).show();
}

}

public class ListenerThread extends AsyncTask<String, Order, Void> {

private Socket socket;
ObjectInputStream ois;

public ListenerThread(Socket socket){
this.socket = socket;
try {
ois = new ObjectInputStream(this.socket.getInputStream()); //hangs here
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

最佳答案

根据The Docs

Threading rules

There are a few threading rules that must be followedfor this class to work properly:

The AsyncTask class must be loaded on the UI thread. This is doneautomatically as of JELLY_BEAN. The task instance must be created onthe UI thread.

您可以通过在 accept() 之后的第一个 AsyncTaskonPostExecute() 中调用新的 Thread 来处理此问题 成功,因为 onPostExecute()UI

上运行

此外,在不进一步查看的情况下,我相信您会希望在第一个任务接受请求后打破 while 循环。然后,当您需要建立另一个连接时,您可以在 UI 中创建此任务的新实例,并再次执行。如果不看更长的时间,我对最后一部分并不乐观,但这似乎是正确的

关于java - 异步任务中的 getInputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15557019/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com