0

In short, look at the "str" variable below. The output of it, is the entire result of the PING operation.

reader = new BufferedReader(new InputStreamReader (process.getInputStream()));
int j;
char[] buffer = new char[240];
StringBuffer output = new StringBuffer();
while ((j = reader.read(buffer)) > 0){
output.append(buffer, 0, j);}
str = output.toString();
Log.d("1:STR VALUE", str);
publishProgress(" "+str+" ");

The "str" variable contains the entire result of the operation, i.e. 3 pings to an IP address. However, I want to display pings line by line, one by one on to the Android activity, from this Async task.

In addition, if someone could explain the performance constraints of the length of the buffer array I am using (i.e. 240 vs say 120), that would be great.

EDIT-1 (response to comment):

11-28 18:11:17.404 10769-10815/com.example1 D/1:STR VALUE: PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
                                                           64 bytes from 192.168.0.1: icmp_seq=1 ttl=255 time=8.83 ms
11-28 18:11:17.600 10769-10815/com.example1 D/1:STR VALUE: PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
                                                           64 bytes from 192.168.0.1: icmp_seq=1 ttl=255 time=8.83 ms
                                                           64 bytes from 192.168.0.1: icmp_seq=2 ttl=255 time=2.05 ms
11-28 18:11:17.801 10769-10815/com.example1 D/1:STR VALUE: PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
                                                           64 bytes from 192.168.0.1: icmp_seq=1 ttl=255 time=8.83 ms
                                                           64 bytes from 192.168.0.1: icmp_seq=2 ttl=255 time=2.05 ms
                                                           64 bytes from 192.168.0.1: icmp_seq=3 ttl=255 time=3.38 ms

                                                           --- 192.168.0.1 ping statistics ---
                                                           3 packets transmitted, 3 received, 0% packet loss, time 403ms
                                                           rtt min/avg/max/mdev = 2.059/4.758/8.835/2.933 ms

Please note that when the STR value is published inside the while loop, repetition of output occurs.

Zac1
  • 208
  • 7
  • 34
  • Could you please post the entire AsyncTask code – foxanna Nov 28 '16 at 22:53
  • Hi, "process" is a runtime process that gets ping results and the "str" variable is published to the main UI via the onProgressUpdate(String... values) method. I can't post the entire code for certain reasons. – Zac1 Nov 28 '16 at 23:00
  • Never mind. Why don't you use `publishProgress(str);` as many times as pings amount, inside the `while` loop. And display lines to Activity inside the `onProgressUpdate` handler. – foxanna Nov 28 '16 at 23:03
  • I moved the following lines inside the while loop, and posted the output as an edit to the question. Please review. [ str = output.toString(); Log.d("1:STR VALUE", str); publishProgress(" "+str+" ");] – Zac1 Nov 28 '16 at 23:15
  • Well, I now added the line "output.delete(0,output.length());" - yet the output is not as line by line to look at... – Zac1 Nov 28 '16 at 23:21
  • You should not publish and delete the whole output value, but rather all the symbols until you meet a line break. – foxanna Nov 28 '16 at 23:28
  • No, as it is in a while loop, i am publishing to the UI and to a file, and then deleting the current buffer. I don't know when the line breaks will be, so I think it publishes till the length of the char buffer array. I set it to 120, so it gets and prints 120 characters, deletes, then prints the next and so on. Is this a good approach? Thanks. – Zac1 Nov 28 '16 at 23:37
  • Hi, I've just found this post http://stackoverflow.com/a/2549222/2452764 You can use same approach with readling and posting output line by line with `while ((line = r.readLine()) != null) ` – foxanna Nov 28 '16 at 23:42
  • If I do that, I get a weird output, things written multiple times. Here is my code - please tell me what I am doing wrong - [ StringBuilder total = new StringBuilder(); while ((line = reader.readLine()) != null) { total.append(line).append('\n'); str = total.toString(); Log.d("1:STR VALUE", str); publishProgress(" "+str+" ");] – Zac1 Nov 29 '16 at 00:27
  • (Weird output is last line of ping (i.e. ping summary) is repeated three times - i.e. num of packets sent=3) – Zac1 Nov 29 '16 at 00:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129296/discussion-between-foxanna-and-zac1). – foxanna Nov 29 '16 at 00:41
  • I joined the chat. Thanks. – Zac1 Nov 29 '16 at 00:48
  • Please post a complete code snippet. Specifically, Java code must be inside a method inside a class. – Code-Apprentice Nov 29 '16 at 00:56

1 Answers1

0

What output do you get when you try this piece of code?

        InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream());
        BufferedReader reader = new BufferedReader(inputStreamReader);
        try {
            String line;
            while ((line = reader.readLine()) != null)
            {
                Log.d("1:STR VALUE", line);
                publishProgress(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

UPDATE: The data you post with publishProgress(line); code will be sent as a parameter to your task's onProgressUpdate method. Append this text to your TextView here.

foxanna
  • 1,570
  • 13
  • 26
  • When I did this, I get the ping results in the log. But nothing in my UI (The textview on Android main has nothing.). – Zac1 Nov 29 '16 at 00:43
  • Yes I have appended it there like this - results1.append(line); - however, the app crashes and throws "java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference at android.widget.TextView.append" [PS: I changed String line; to String line=" ";] - still no luck. – Zac1 Nov 29 '16 at 01:00
  • I caught the exception, BUT the results in the Android UI are bad. Some lines are repeated twice, for example "rtt min/avg/max/mdev = " immediately followed again by the same line - "rtt min/avg/max/mdev = " – Zac1 Nov 29 '16 at 01:13