【问题标题】:onProgressUpdate doesn't update each time server receive dataonProgressUpdate 不会在每次服务器接收数据时更新
【发布时间】:2015-05-29 05:09:08
【问题描述】:

我曾经使用AsyncTask建立服务器socket连接,然后接受客户端连接,最后每次客户端向服务器发送数据都需要更新GUI。

但是当客户端发送数据时,GUI 没有更新,如果客户端断开连接,那么 GUI 将更新。当客户端建立三个连接然后断开连接时会发生这种情况,然后 GUI 将被更新。但之后 GUI 将不会更新,然后我看不到客户端发送到服务器的数据。如何解决这个问题。

public class ServerSocket_test extends Activity {

 private TextView textView;
    private EditText editIP;
    private EditText editMessage;
    private Socket socketClient=null;
    private PrintWriter printWriterClient=null;
    public static final int LISTEN_PORT = 6000;


    private class TaskServerSocket extends AsyncTask<Void, Void, Void> {
        @SuppressLint("NewApi")
        @Override
        protected Void doInBackground(Void... v) {
            try {
                ServerSocket serverSocket = new ServerSocket(LISTEN_PORT);
                while (true) {
                    socketClient = serverSocket.accept();
                    TaskRead taskRead = new TaskRead();
                    taskRead.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, socketClient);
                }
            } catch (IOException ex) {
                Log.e("SocketsChat", ex.getLocalizedMessage());
            }
            return null;
        }
    }



    private class TaskRead extends AsyncTask<Socket,String,Void> {

        String in="";
        @Override
        protected Void doInBackground(Socket... socketClient) {
            String s;
            try {
                 BufferedReader input = new BufferedReader(
                                new InputStreamReader(socketClient[0].getInputStream()));
                 while (true){ //(socketClient[0].isConnected()) {
                        while ((s=input.readLine())!=null) {

                            publishProgress(s, socketClient[0].getInetAddress().toString());
                        }
                }
            } catch (IOException exc) {
                Log.d("SocketChat", exc.getLocalizedMessage());
            }
            return null;
        }

       @Override
        protected void onProgressUpdate(String... strings) {
           textView.append(strings[1]+": "+strings[0]+"\n");
            in=in+strings;
        }


      @Override
        protected void onPostExecute(Void result){
            textView.append(in);
        }

    }

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_server_socket_test);

     textView = (TextView) findViewById(R.id.textView1);

     TaskServerSocket taskServerSocket = new TaskServerSocket();
    taskServerSocket.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);


}  }

【问题讨论】:

    标签: android sockets android-asynctask serversocket


    【解决方案1】:

    AsyncTask 必须在 UI(主)线程中启动。

    在您的代码中,您在 TaskServerSocket 的 doInBackground 方法中启动 TaskRead。 方法 doInBackground 不在 UI(主)线程中。

    查看文档:http://developer.android.com/reference/android/os/AsyncTask.html

    “AsyncTask 类必须在 UI 线程上加载”

    另一点,2 AsyncTasks,在你的情况下,很奇怪,似乎没有必要。最好合并两个 AsyncTask。

    【讨论】:

    • @Hanaa,有用吗?
    猜你喜欢
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    相关资源
    最近更新 更多