Android的插座:连续读取数据 [英] Android Socket: Read Continuous Data

查看:152
本文介绍了Android的插座:连续读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想继续使用以下code读取数据:

 公共类MyClientTask扩展的AsyncTask<太虚,太虚,太虚> {        串dstAddress;
        INT dstPort;
        串响应=;        MyClientTask(字符串地址,端口INT){
            dstAddress =地址;
            dstPort =口;        }        @覆盖
        保护无效doInBackground(虚空......为arg0){            Socket套接字= NULL;            尝试{
                插座=新的Socket(dstAddress,dstPort);                ByteArrayOutputStream byteArrayOutputStream =新ByteArrayOutputStream(1024);
                字节[]缓冲区=新的字节[1024];                INT读取动作;
                为InputStream的InputStream = socket.getInputStream();
                而((读取动作= inputStream.read(缓冲液))!= - 1){
                    readInpt = inputStream.toString();
                    byteArrayOutputStream.write(缓冲液,0,读取动作);
                    响应= byteArrayOutputStream.toString(UTF-8);
                }
                textResponse.setText(readInpt);            }赶上(UnknownHostException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
                响应=UnknownHostException异常:+ e.toString();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
                响应=IOException异常:+ e.toString();
            }最后{
                如果(插座!= NULL){
                    尝试{
                        socket.close();
                    }赶上(IOException异常五){
                        // TODO自动生成catch块
                        e.printStackTrace();
                    }
                }
            }
            返回null;
        }        @覆盖
        保护无效onPostExecute(虚空结果){
            textResponse.setText(响应);
            super.onPostExecute(结果);
        }    }

但由于某些原因,它不显示我在文本框中任何输出。任何帮助将是AP preciated。


解决方案

  {尝试
                插座=新的Socket(dstAddress,dstPort);                的BufferedReader标准输入=新的BufferedReader(新的InputStreamReader(socket.getInputStream()));                而(真){
                    响应= stdIn.readLine();
                    publishProgress(响应);
                    Log.i(回应,响应);                }            }赶上(UnknownHostException异常五){
                // TODO自动生成catch块                e.printStackTrace();
                响应=UnknownHostException异常:+ e.toString();
            }赶上(IOException异常五){
                // TODO自动生成catch块                e.printStackTrace();
                响应=IOException异常:+ e.toString();
            }            赶上(例外五){                e.printStackTrace();
            }

您不能打印文本字段,因为套接字将听取服务器socket.If服务器套接字不发送它会听不断,直到收到响应插座任何回应。

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.

解决方案

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.

这篇关于Android的插座:连续读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆