Android Socket:读取连续数据

时间:2014-03-10 19:55:46

标签: 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;
            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);
        }

    }

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

2 个答案:

答案 0 :(得分:0)

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

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

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

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

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

答案 1 :(得分:0)

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

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