如何使用aynctask连续读取套接字中的数据

时间:2018-04-20 03:27:06

标签: java android sockets android-studio

我是android的新手,我正在尝试使用异步任务从套接字读取数据。但是从套接​​字读取数据一次后,AsyncTask就会被终止。有没有办法连续执行AsyncTask?

目前,从下面的代码中,我只能在屏幕上显示一次数据。但是,只要新数据进入套接字,我就需要连续显示数据。

public class Client extends AsyncTask<Void, Void, Integer> {

        String dstAddress;
        int dstPort;
        String response = "";
        TextView textResponse;
        int value;

        Client(String addr, int port, TextView textResponse) {
        dstAddress = addr;
        dstPort = port;
        this.textResponse = textResponse;
        }

@Override
protected Integer doInBackground(Void... arg0) {

        Socket socket = null;

        try {
        socket = new Socket(dstAddress, dstPort);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
        1024);
        byte[] buffer = new byte[1024];
                byte ch = '1';
                Character c;
                DataOutputStream dout=new DataOutputStream(socket.getOutputStream());
                // dout.writeUTF(ch);
                dout.write(ch);
                dout.flush();
        int bytesRead;
                InputStream input = socket.getInputStream();
                DataInputStream in = new DataInputStream(input);
                byte buf[] = new byte[1000];


                        input = socket.getInputStream();
                        in = new DataInputStream(input);
                        System.out.print(in.readInt());
                        value= in.readInt();
                        System.out.print(value);
                        dout.write(ch);
                        dout.flush();
//                in.read(buf);
//                for(byte b:buf)
//                {
//                    c = (char)b;
//                    System.out.print(c);
//
//            }}


        } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.print("UnknownHostException: " + e.toString());
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.print("IOException: " + e.toString());
//        } finally {
//        if (socket != null) {
//        try {
//        socket.close();
//        } catch (IOException e) {
//        // TODO Auto-generated catch block
//        e.printStackTrace();
//        }
//        }
        }
        return value;
        }

@Override
protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        Log.d("Exception","result"+result);
        textResponse.setText("" +result);


        }

        }

我使用处理程序来调用同步任务,但它没有用。下面显示了处理程序的代码。

myClient = new Client(hostname, port,tvCount);
         Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                myClient.execute();
                //Do something after 100ms
            }
        }, 5000);

2 个答案:

答案 0 :(得分:1)

你可以试试这个,

Handler handler = new Handler();

Runnable runnable = new Runnable(){
    public void run(){
        new Client(hostname, port,tvCount).execute(); 
        //do something
        handler.postDelayed(this, 5000);
    }
};
runnable.run();

答案 1 :(得分:0)

.flex-grow {
  flex: 1 0 auto;
}

在您的Service类中尝试此操作因为它会从您的应用启动器活动或您希望的某些基本活动启动后连续运行。这是一个从服务器Socket接收消息的工作解决方案。 Services in Android