Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android套接字:读取连续数据_Android_Sockets_Android Asynctask - Fatal编程技术网

Android套接字:读取连续数据

Android套接字:读取连续数据,android,sockets,android-asynctask,Android,Sockets,Android Asynctask,我尝试使用以下代码连续读取数据: public class MyClientTask extends AsyncTask<Void, Void, Void> { String dstAddress; int dstPort; String response = ""; MyClientTask(String addr, int port){ dstAddress = addr;

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

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方法是否有效,因为文档中说它返回的对象描述与接收到的字符串不同。您可能会将其与缓冲区的内容相混淆,而缓冲区的内容可能正是您真正想要的

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