0

私はこのコードを持っており、ダウンロードしたデータの量を教えてくれ(そしてすぐにプログレスバーに入れて)、Saxパーサーで結果を解析できるようになることを望んでいます。基本的に//xr.parse(newInputSource(request.getInputStream()));より上のすべてをコメントアウトするとします。xr.parseの行を入れ替えて、正常に動作します。しかし、現時点では、私の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. */;

誰か助けてもらえますか?

敬具、

アンディ

4

4 に答える 4

1

「別の方法を知らない限り、バイトでしかそれを行う方法を見つけることができませんでした?」.

しかし、あなたは方法を見つけていません。動作しないコードを書きました。また、入力を文字列に保存したくない場合もあります。解析中にバイト数をカウントしたいとします。そうしないと、遅延が増えるだけです。つまり、時間を浪費し、すべてが遅くなります。正しい方法の例については、javax.swing.ProgressMonitorInputStream を参照してください。それを使用する必要はありませんが、確かに何らかの種類の FilterInputStream を使用する必要があります。おそらく自分で作成したもので、リクエスト入力ストリームをラップしてパーサーに渡します。

于 2010-03-26T04:32:41.627 に答える
0

以前にデータを消費するInputStream別のものを構築しています。InputStream

単一バイトだけを読み取らないようにしたい場合は、 aBufferedInputStreamまたは a のような別のものを使用できますBufferedReader

いずれにしても、解析する前にコンテンツ全体を取得することをお勧めします! 動的に解析する必要がない限り。

あなたが本当にやっているようにそれを続けたいのであれば、2 つのパイプされたストリームを作成する必要があります:

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

pipeIn.connect(pipeOut);

pipeOut.write(yourBytes);

xr.parse(pipeIn);

Java の Streams は、その名前が示すように、正確な次元を持たず、いつ終了するかもわからないため、InputStream.InputStream以前のものから消費されます。

両方のこと (ダウンロードと解析) を一緒に実行したい場合は、 から受信したデータをフックするHTTPUrlConncection必要があります。

  • 最初にダウンロードされるデータの長さを知ってください。これはHttpUrlConnectionヘッダーから取得できます
  • 装飾するカスタム InputStream を使用する (これが Java でストリームが機能する方法です。こちらを参照してください) プログレスバーを更新します。

何かのようなもの:

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);
于 2010-03-26T01:40:12.247 に答える
0

while ループは入力ストリームを消費しており、パーサーが読み取るものは何も残していません。

あなたがしようとしていることについては、入力ストリームをラップする FilterInputStream サブクラスの実装を検討することをお勧めします。

于 2010-03-26T01:32:04.713 に答える
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));
}
于 2012-09-24T05:56:39.243 に答える