2010-03-26 29 views
0

我有這段代碼,我希望能夠告訴我有多少數據我已經下載(並很快把它放在進度條中),然後通過我的薩克斯解析器解析結果。如果我基本上註釋了上面的所有內容,請參閱//x.parse(new InputSource(request.getInputStream()));行並交換xr.parse的結束,它工作正常。但目前,我的薩克斯解析器告訴我我什麼都沒有。這是與事情有關嗎? 閱讀(緩衝區)部分?爲什麼在使用InputStream Read之後,我的Sax解析器不會產生結果?

此外,就像一個說明,請求是一個帶有各種簽名的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. */; 

任何人都可以幫助我嗎?

親切的問候,

安迪

回答

1

「我只能找到一個方法來做到這一點 以字節爲單位,除非你知道另一種方法 ?」。

但你還沒有找到了一種方法。你剛纔編寫的代碼不起作用。而且您不希望將輸入保存爲字符串。你想在解析它們時計算字節否則,你只是增加延遲,即浪費時間並放慢一切。有關如何正確執行的示例,請參閱javax.swing.ProgressMonitorInputStream。您不必使用它,但您肯定必須使用某種類型的FilterInputStream,您自己編寫的probaby一個,它包裝在請求輸入流中並傳遞給解析器。

+0

+1指向實際進行監控的源代碼,但他需要編寫非swing版本。 – 2010-03-26 10:10:57

0

你while循環消耗輸入流並留下任何解析器讀取。

對於你要做的事情,你可能想要考慮實現一個包裝輸入流的FilterInputStream子類。

+0

聽起來很複雜!任何我可以找到例子的地方? – Schodemeiss 2010-03-26 01:34:17

+0

其實並不是那麼複雜。 有關於示例的一個stackoverflow問題 - 請參閱http://stackoverflow.com/questions/765473/examples-of-java-io-stream-filters – 2010-03-26 01:51:09

+0

關於如何找到示例的簡短答案是找到您的src.jar Java SDK安裝。正如在另一個回答中指出的那樣,javax.swing.ProgressMonitorInputStream是一個很好的示例,可以完全按照您的要求進行操作,但在Swing上下文中。 – 2010-03-26 10:13:56

0

您正在構建一個InputStream而其他InputStream之前會消耗其數據。

如果你想避免只讀單個字節,你可以使用BufferedInputStream或不同的東西,如BufferedReader

在任何情況下,最好在解析它之前獲取整個內容!除非你需要動態解析它。

如果你真的想保持它就像你正在做的,你應該創建兩個管道流:

PipedOutputStream pipeOut = new PipedOutputStream(); 
PipedInputStream pipeIn = new PipedInputStream(); 

pipeIn.connect(pipeOut); 

pipeOut.write(yourBytes); 

xr.parse(pipeIn); 

流在Java中,像他們的名字建議你,沒有一個準確的尺度也不知道當他們完成時,無論何時創建InputStream,如果您從中讀取數據,則不能將相同的InputStream傳遞給另一個對象,因爲數據已從前一個對象中消耗。

如果你想要做的兩件事(下載和解析)在一起,你必須從HTTPUrlConncection您應該接收到的數據之間的掛鉤:

  • 先知道被下載的數據的長度,這可能是從使用裝飾定製的InputStream HttpUrlConnection
  • 獲得(這是如何在Java流工作,看到here)updading的進度..

喜歡的東西:

class MyInputStream extends InputStream 
{ 
    MyInputStream(InputStream is, int total) 
    { 
    this.total = total; 
    } 

    public int read() 
    { 
    stepProgress(1); 
    return super.read(); 
    } 

    public int read(byte[] b) 
    { 
    int l = super.read(b); 
    stepProgress(l); 
    return l; 
    } 

    public int read(byte[] b, int off, int len) 
    { 
    int l = super.read(b, off, len); 
    stepProgress(l); 
    return l 
    } 
} 



InputStream mis= new MyInputStream(request.getInputStream(), length); 
.. 
xr.parse(mis); 
+0

那麼,我知道所有返回的數據將是XML如果有任何幫助。能夠看到下載的進度會很高興,我只能找到一種方法來用字節來實現,除非你知道另一種方法? – Schodemeiss 2010-03-26 01:44:03

+0

我怎樣才能保存第一次運行得到它的讀取多少,保存到一個字符串......因爲我確信InputSource接受字符串! – Schodemeiss 2010-03-26 01:44:43

0

您可以將您的數據保存在一個文件中,然後將其讀出。

InputStream is = request.getInputStream(); 
if(is!=null){ 
File file = new File(path, "someFile.txt"); 
FileOutputStream os = new FileOutputStream(file); 
buffer = new byte[2048]; 
bufferLength = 0; 
    while ((bufferLength = is.read(buffer)) > 0) 
    os.write(buffer, 0, bufferLength); 

os.flush(); 
os.close(); 
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
factory.setNamespaceAware(true); 
XmlPullParser xpp = factory.newPullParser(); 
FileInputStream fis = new FileInputStream(file); 
xpp.setInput(new InputStreamReader(fis)); 
} 
相關問題