当前位置: 代码迷 >> Android >> Android套接字:读取连续数据
  详细解决方案

Android套接字:读取连续数据

热度:75   发布时间:2023-08-04 12:47:10.0

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

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

    }

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

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

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

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

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

textResponse.setText(readInpt); // Forbidden, move somewhere else, e.g. onPostExecute()
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();
            }

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

  相关解决方案