【问题标题】:Android Socket: Read Continuous DataAndroid Socket:读取连续数据
【发布时间】:2014-04-14 03:16:17
【问题描述】:

我正在尝试使用以下代码连续读取数据:

public class MyClientTask extends AsyncTask<Void, Void, Void> {

        String dstAddress;
        int dstPort;
        String response = "";

        MyClientTask(String addr, int port){
            dstAddress = addr;
            dstPort = port;

        }

        @Override
        protected Void doInBackground(Void... arg0) {

            Socket socket = null;

            try {
                socket = new Socket(dstAddress, dstPort);

                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
                byte[] buffer = new byte[1024];

                int bytesRead;
                InputStream inputStream = socket.getInputStream();


                while ((bytesRead = inputStream.read(buffer)) != -1){
                    readInpt = inputStream.toString();
                    byteArrayOutputStream.write(buffer, 0, bytesRead);
                    response = byteArrayOutputStream.toString("UTF-8");
                }
                textResponse.setText(readInpt);

            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                response = "UnknownHostException: " + e.toString();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                response = "IOException: " + e.toString();
            }finally{
                if(socket != null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            textResponse.setText(response);
            super.onPostExecute(result);
        }

    }

但由于某种原因,它没有在文本框中显示任何输出。任何帮助将不胜感激。

【问题讨论】:

    标签: android sockets android-asynctask


    【解决方案1】:

    您的代码中至少有两个问题。

    首先,我不确定inputStream 上的方法toString() 是否有效,因为文档说它返回对象的描述(这与收到的字符串不同)。您可能会将其与 buffer 的内容混淆,这可能是您真正想要的。

    readInpt = inputStream.toString();  // Probably wrong
    

    第二。您正在从 doInBackground() 内部的后台线程更新用户界面,这始终是被禁止的。

    textResponse.setText(readInpt); // Forbidden, move somewhere else, e.g. onPostExecute()
    

    【讨论】:

      【解决方案2】:
      try {
                      socket = new Socket(dstAddress, dstPort);
      
                      BufferedReader stdIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      
                      while (true) {
                          response = stdIn.readLine();
                          publishProgress(response);
                          Log.i("response", response);
      
                      }
      
                  } catch (UnknownHostException e) {
                      // TODO Auto-generated catch block
      
                      e.printStackTrace();
                      response = "UnknownHostException: " + e.toString();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
      
                      e.printStackTrace();
                      response = "IOException: " + e.toString();
                  }
      
                  catch (Exception e) {
      
                      e.printStackTrace();
                  }
      

      您不能在文本字段上打印它,因为套接字将侦听服务器套接字。如果服务器套接字不发送任何响应,它将连续侦听套接字,直到收到响应为止。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-24
        • 2020-05-18
        • 2016-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多