异步任务中的getInputStream

时间:2013-03-21 20:06:15

标签: java android sockets android-asynctask socketserver

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

Async任务调用另一个Async任务来侦听消息。但是它挂起了get输入流

我已经绞尽脑汁待了好几个小时,无法解决为什么它会挂起......请帮忙。

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();
        }
    }

2 个答案:

答案 0 :(得分:1)

根据 The Docs

  

线程规则

     

必须遵循一些线程规则   让这个班级正常工作:

     

必须在UI线程上加载AsyncTask类。这个完成了   自JELLY_BEAN起。必须创建任务实例   UI线程。

您可以在Thread成功后在onPostExecute() AsyncTask accept()中调用新的onPostExecute()来处理此问题,因为UIwhile loop上运行UI

此外,如果不再多看,我相信您会在接受请求后在第一个任务中突破execute。然后,当您需要建立另一个连接时,您可以再次在{{1}}和{{1}}中创建此任务的新实例。我没有看到最后一部分没有看到它更长但是看起来是正确的

答案 1 :(得分:0)

我不确定,但我猜你只能从UI线程调用AsyncTask.execute(),并且你试图从doInBackground()中执行它,它在单独的线程中运行。