为什么我的 Sax 解析器在使用 InputStream Read 后没有产生结果?
我有这段代码,我希望它能够告诉我已经下载了多少数据(并很快将其放入进度条中),然后通过我的 Sax 解析器解析结果。如果我基本上注释掉 //xr.parse(new InputSource(request.getInputStream())); 行上方的所有内容并交换 xr.parse 的内容,则它可以正常工作。但目前,我的 Sax 解析器告诉我我什么都没有。这与 is.read (缓冲区)部分有关吗?
另外,请注意,请求是具有各种签名的 HttpURLConnection。
/*Input stream to read from our connection*/
InputStream is = request.getInputStream();
/*we make a 2 Kb buffer to accelerate the download, instead of reading the file a byte at once*/
byte [ ] buffer = new byte [ 2048 ] ;
/*How many bytes do we have already downloaded*/
int totBytes,bytes,sumBytes = 0;
totBytes = request.getContentLength () ;
while ( true ) {
/*How many bytes we got*/
bytes = is.read (buffer);
/*If no more byte, we're done with the download*/
if ( bytes <= 0 ) break;
sumBytes+= bytes;
Log.v("XML", sumBytes + " of " + totBytes + " " + ( ( float ) sumBytes/ ( float ) totBytes ) *100 + "% done" );
}
/* Parse the xml-data from our URL. */
// OLD, and works if comment all the above
//xr.parse(new InputSource(request.getInputStream()));
xr.parse(new InputSource(is))
/* Parsing has finished. */;
任何人都可以帮助我吗?
亲切的问候,
安迪
I have this piece of code which I'm hoping will be able to tell me how much data I have downloaded (and soon put it in a progress bar), and then parse the results through my Sax Parser. If I comment out basically everything above the //xr.parse(new InputSource(request.getInputStream())); line and swap the xr.parse's over, it works fine. But at the moment, my Sax parser tells me I have nothing. Is it something to do with is.read (buffer) section?
Also, just as a note, request is a HttpURLConnection with various signatures.
/*Input stream to read from our connection*/
InputStream is = request.getInputStream();
/*we make a 2 Kb buffer to accelerate the download, instead of reading the file a byte at once*/
byte [ ] buffer = new byte [ 2048 ] ;
/*How many bytes do we have already downloaded*/
int totBytes,bytes,sumBytes = 0;
totBytes = request.getContentLength () ;
while ( true ) {
/*How many bytes we got*/
bytes = is.read (buffer);
/*If no more byte, we're done with the download*/
if ( bytes <= 0 ) break;
sumBytes+= bytes;
Log.v("XML", sumBytes + " of " + totBytes + " " + ( ( float ) sumBytes/ ( float ) totBytes ) *100 + "% done" );
}
/* Parse the xml-data from our URL. */
// OLD, and works if comment all the above
//xr.parse(new InputSource(request.getInputStream()));
xr.parse(new InputSource(is))
/* Parsing has finished. */;
Can anyone help me at all??
Kind regards,
Andy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
但您还没有找到方法。您刚刚编写了不起作用的代码。而且您也不想将输入保存到字符串中。您希望在解析字节时计算字节数。否则您只会增加延迟,即浪费时间并减慢一切速度。有关如何正确执行此操作的示例,请参阅 javax.swing.ProgressMonitorInputStream。您不必使用它,但您肯定必须使用某种 FilterInputStream,可能是您自己编写的,它包裹在请求输入流中并传递给解析器。
But you haven't found a method. You've just written code that doesn't work. And you don't want to save the input to a String either. You want to count the bytes while you're parsing them. Otherwise you're just adding latency, i.e. wasting time and slowing everything down. For an example of how to do it right, see javax.swing.ProgressMonitorInputStream. You don't have to use that but you certainly do have to use a FilterInputStream of some sort, probaby one you write yourself, that is wrapped around the request input stream and passed to the parser.
您的 while 循环正在消耗输入流,并且没有留下任何内容供解析器读取。
对于您想要做的事情,您可能需要考虑实现包装输入流的 FilterInputStream 子类。
Your while loop is consuming the input stream and leaving nothing for the parser to read.
For what you're trying to do, you might want to look into implementing a FilterInputStream subclass wrapping the input stream.
您正在另一个之前消耗其数据的
InputStream
上构建一个InputStream
。如果您想避免只读取单个字节,您可以使用 BufferedInputStream 或不同的东西,例如 BufferedReader 。
无论如何,最好在解析之前获取全部内容!除非你需要动态解析它。
如果你真的想像现在一样保持它,你应该创建两个管道流:
Java 中的流,就像它们的名字所暗示的那样,没有精确的维度,你也不知道它们何时完成,所以每当你创建一个 < code>InputStream,如果您从中读取数据,则无法将相同的
InputStream
传递给另一个对象,因为数据已经从前一个对象中被消耗。如果您想同时执行这两项操作(下载和解析),则必须在从
HTTPUrlConncection
接收到的数据之间进行挂钩,您应该:类似:
You are building an
InputStream
over anotherInputStream
that consumes its data before.If you want to avoid reading just single bytes you could use a
BufferedInputStream
or different things like aBufferedReader
.In any case it's better to obtain the whole content before parsing it! Unless you need to dynamically parse it.
If you really want to keep it on like you are doing you should create two piped streams:
Streams in Java, like their name suggest you, doesn't have a precise dimension neither you know when they'll finish so whenever you create an
InputStream
, if you read from them you cannot then pass the sameInputStream
to another object because data is already being consumed from the former one.If you want to do both things (downloading and parsing) together you have to hook between the data received from the
HTTPUrlConncection
you should:HttpUrlConnection
headerSomething like:
您可以将数据保存在文件中,然后将其读出。
You can save your data in a file, and then read them out.