0

I have a code that sends data on click. However, I want to send data repeatedly with a fixed time interval.

public void onClick(View view) {
        try {
            EditText et = (EditText) findViewById(R.id.EditText01);
            String str = et.getText().toString();
            PrintWriter out = new PrintWriter(new BufferedWriter(
                    new OutputStreamWriter(socket.getOutputStream())),
                    true);
            out.println(str);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    class ClientThread implements Runnable {

        @Override
        public void run() {

            try {
                InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

                socket = new Socket(serverAddr, SERVERPORT);

            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }

    }

any help would be appreciated.

MKS
  • 736
  • 2
  • 14
  • 33
  • You shouldn't create a new `PrintWriter`, `BufferedWriter`, or `OutputStreamWriter` per message. They should all exist for the life of the socket. And you shouldn't execute networking code on the event thread. – user207421 Mar 11 '14 at 00:18
  • Thanks for reply. I also tried with AsyncTask (http://stackoverflow.com/questions/22309994/android-socket-read-continuous-data/22310320?noredirect=1#22310320), but for some reason, it didn't work. – MKS Mar 11 '14 at 02:02

1 Answers1

0

Create a new thread with a while loop and a Thread.sleep() call. This post has all the code you need: How to run a Runnable thread in Android?

Community
  • 1
  • 1
Erik B
  • 2,810
  • 1
  • 34
  • 38
  • I change the code, but for reason it shows null pointer exception; here is the updated code: final Runnable r = new Runnable() { public void run() { try { String str = "test"; PrintWriter out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())), true); out.println(str); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }; – MKS Mar 10 '14 at 22:11
  • Unfortunately the code is hard to read and I don't think I'm seeing all of it. Maybe you could paste your code in https://gist.github.com/ or pastebin.com and add a link here. Also it helps to provide the stacktrace error. – Erik B Mar 10 '14 at 22:46
  • 1
    @MKS You can see for yourself that posting code in comments is futile. It is quite illegible. If you have a new question, post it, with that code, and the stack trace *and the code concerned* and an indication of which line throws the exception. There's nothing there that throws a `NullPointerException` unless `socket` is null, but you should have been able to work that out for yourself. – user207421 Mar 11 '14 at 00:20