简体   繁体   English

试图理解Android中的XML Pull Parser - Java

[英]Trying to understand XML Pull Parser in Android - Java

I was following a tutorial for reading RSS feeds using XML. 我正在学习使用XML阅读RSS feed的教程。 Please know that I am a very visual learner and the person teaching tried to explain it in words but sadly I didn't understand :(. So I'm hoping someone can explain. 请知道我是一个非常直观的学习者,教学人员试图用语言解释它,但遗憾的是我不明白:(。所以我希望有人可以解释。

 StringBuilder tempBuffer = new StringBuilder();

        try{
            URL xmlUrl = new URL(URLPath);
            HttpURLConnection httpURLConnection = (HttpURLConnection) xmlUrl.openConnection();
            int response = httpURLConnection.getResponseCode();
            Log.d("Downloaded Data", " Response Code:" + response);

            InputStream is = httpURLConnection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);

            int charRead;
            char[] buffer = new char[500];

            while(true){

                charRead = isr.read(buffer);

                if(charRead <= 0){
                    break;
                }


                tempBuffer.append(String.copyValueOf(buffer, 0, charRead));

            }
            return tempBuffer.toString();

Now the thing I want to try to understand is the char[] buffer = new char[500]. 现在我想要尝试理解的是char [] buffer = new char [500]。 What does it mean when we do charRead = isr.read(buffer), in regard to reading the stream? 在读取流时,我们做charRead = isr.read(缓冲区)是什么意思?

It reads up to 500 bytes (the length of the buffer) from the stream and puts it in buffer. 它从流中读取最多500个字节(缓冲区的长度)并将其放入缓冲区。 charRead is the number of characters it actually read- somewhere from 0 to 500, depending on how much data was waiting. charRead是它实际读取的字符数 - 从0到500,取决于等待的数据量。 charRead can also be less than 0 in case of an error. 如果发生错误,charRead也可以小于0。

I'll try to explain it as a visual example. 我将尝试将其解释为一个视觉示例。

Imagine an InputStream as a conveyor belt coming out of a hole in the wall (the network). 想象一下, InputStream是一个从墙上的洞(网络)出来的传送带。 There is a series of char s on the conveyor belt, but you need to bring them to you before you can take them off the conveyor belt and process them into your bucket ( buffer , which can only hold 500 char ). 传送带上有一系列char ,但您需要将它们带到您身边才能将它们从传送带上取下并将它们加工到您的桶中( buffer ,其中只能容纳500个char )。 When you call isr.read(buffer) , the conveyor belt operator is told to start the conveyor belt. 当您调用isr.read(buffer) ,传送带操作员会被告知启动传送带。 The conveyor belt will start to roll, dumping the char objects into the bucket ( buffer ). 传送带将开始滚动,将char对象倾倒入桶( buffer )。 This will continue until either 这将持续到任何一个

A. The conveyor belt dumps 500 chars into the bucket ( buffer ), or A.传送带将500 chars转储到桶( buffer )中,或

B. The conveyor belt runs out of chars coming out of the hole in the wall (network). B.传送带从墙上的孔(网络)中出来的chars用完。

The line of code: 代码行:

if(charRead <= 0){
    break;
}

Tells the conveyor belt that if NOTHING came out of the hole in the wall when the button was pressed to stop pressing the button and exit the loop. 告诉传送带,当按下按钮时,如果没有从墙上的孔中出来,则停止按下按钮并退出回路。

The line of code: 代码行:

tempBuffer.append(String.copyValueOf(buffer, 0, charRead));

tells the conveyor belt operator to stop what he's doing (after the conveyor belt stopped moving because either 500 chars got dumped into the bucket or the conveyor belt ran out of chars ) and take everything that's in the bucket ( buffer ), and take out charRead amount of chars . 告诉传送带操作员停止他正在做什么(在传送带停止移动之后,因为500个chars被倾倒到桶中或传送带用完了chars )并取出桶中的所有内容( buffer ),并取出charReadchars Basically, this is everything that's in the bucket, because we know that charRead is how many chars came off the conveyor belt and went into the bucket, so if we take charRead chars from the bucket, we know that we got all of them. 基本上,这就是桶中的所有东西,因为我们知道charRead是从传送带上带出多少个chars并进入桶中,所以如果我们从桶中取出charRead chars ,我们知道我们得到了所有这些chars These are appended onto the StringBuilder , and the loop starts over again! 这些被附加到StringBuilder ,循环重新开始!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM