2010-07-11 69 views
2

我想要做的就是登錄我去使用使用用於記錄一個InputStream,然後XML解析

org.apache.http.HttpEntity entity = response.getEntity(); 
org.apache.http.HttpResponse content =entity.getContent(); 


      //Print the result to the screen for debugging 
      //puroposes 
      if(Logging.DEBUG) { 
       InputStream content =entity.getContent(); 

       int i; 
       StringBuilder b = new StringBuilder(); 
       while((i=content.read()) != -1) { 
        b.append((char)i); 
       } 

       Log.d(TAG, b.toString()); 
      } 

從InputStream輸出現在我已經完成登錄後,我想用完全相同的流通過一個XML解析器。問題是它告訴我蒸汽已經被使用了。

我試過在調試之前和之後調用mark()reset(),但它不起作用。

回答

2

這取決於返回的inputstream是否支持它。 InputStream類中的默認實現不會執行任何操作,因爲described in the API。所以你不能確定返回的Stream是否真的支持它。爲了確保這一點,你應該把它包裝在支持這些方法的BufferedInputStream中。

+0

這工作,謝謝 – jax 2010-07-11 05:25:22

+0

也許這個解決方案可能對某人有所幫助:http://shomeser.blogspot.com/2013/12/redirect-stream-to-logcat.html – 2013-12-24 12:36:13

0

一般而言,mark()reset()不適用於任意InputStream。他們只能在FileInputStream這樣的子類中工作,其中底層數據源支持這些操作。

對於類似於SocketInputStream或控制檯InputStream的東西,您唯一的選擇是讀取並緩衝整個流內容;例如在內存中或通過將其寫入臨時文件。