【发布时间】:2016-11-28 22:39:11
【问题描述】:
简而言之,看看下面的“str”变量。它的输出是 PING 操作的整个结果。
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+" ");
“str”变量包含操作的全部结果,即 3 次 ping 到一个 IP 地址。但是,我想从这个异步任务中逐行显示 ping 到 Android 活动。
此外,如果有人能解释我正在使用的缓冲区数组长度的性能限制(即 240 与 120),那就太好了。
EDIT-1(回复评论):
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
请注意,当 STR 值在 while 循环内发布时,会出现重复输出。
【问题讨论】:
-
能否请您发布整个 AsyncTask 代码
-
嗨,“process”是一个运行时进程,它获取 ping 结果,“str”变量通过 onProgressUpdate(String... values) 方法发布到主 UI。由于某些原因,我无法发布整个代码。
-
没关系。为什么不在
while循环内使用与ping 数量一样多的publishProgress(str);。并在onProgressUpdate处理程序中向 Activity 显示行。 -
我在 while 循环中移动了以下几行,并将输出作为问题的编辑发布。请查阅。 [ str = output.toString(); Log.d("1:STR 值", str);发布进度(" "+str+" ");]
-
好吧,我现在添加了行“output.delete(0,output.length());” - 但输出不是逐行查看...
标签: java android inputstream ping android-networking