简体   繁体   English

Android套接字:读取连续数据

[英]Android Socket: Read Continuous Data

I am trying to read data continuously using the following code: 我正在尝试使用以下代码连续读取数据:

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

    }

But for some reason, it doesn't show me any output in the textbox. 但是由于某种原因,它在文本框中没有显示任何输出。 any help would be appreciated. 任何帮助,将不胜感激。

There are at least two issues in your code. 您的代码中至少有两个问题。

Frist, I'm not sure the method toString() on the inputStream is going to work, because the documentation says it returns a description of the object (which would be different than the string recieved). 第一,我不确定inputStream上的toString()方法是否可以正常工作,因为文档说它返回了对象的描述(这与收到的字符串不同)。 You might be confusing this with the contents of buffer which might be what you really want. 您可能会将其与buffer的内容混淆,这可能正是您真正想要的。

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

Second. 第二。 You're updating the User Interface from a background thread, inside doInBackground() , which is always forbidden. 您正在从后台线程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();
            }

You cannot print it on text field because the socket will listen to the server socket.If server socket does not send any response it will listen to the socket continuously until the response is received. 您不能在文本字段上打印它,因为套接字将监听服务器套接字。如果服务器套接字未发送任何响应,它将连续监听套接字,直到收到响应为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM