开发者

Best practices retrieving XML/stream from HTTP in Android

开发者 https://www.devze.com 2022-12-29 07:12 出处:网络
what are the best practic开发者_如何转开发es parsing XML from an HTTP resource in Android? I\'ve been using HttpURLConnection to retrieve an InputStream, wrapping it with a BufferedInputStream, and th

what are the best practic开发者_如何转开发es parsing XML from an HTTP resource in Android? I've been using HttpURLConnection to retrieve an InputStream, wrapping it with a BufferedInputStream, and then using SAX to parse the buffered stream. For the most part it works, though I do receive error reports of SocketTimeoutException: The operation timed out or general parsing error. I believe it's due to the InputStream.

  1. Would using HttpClient instead of HttpURLConnection help? If yes, why?
  2. Should the stream be output to a file, having the file parsed instead of the stream?

Any input or direction would be greatly appreciated. Thanks for your time.


I recently did this so I don't have a ton of experience with it, but I used HTTPCLient. HTTPClient does a few things better than raw URLConnections, with the most important in this case being that it retries three times if it cannot connect to the server. This helps if you just have a crappy connection with packet loss.

I also parse the InputStream with SAX, but I wrap the InputStream in a BufferedReader (using an intermediate InputStreamReader). I have no idea if SAX does this internally, but from my experience, BufferedStreams work considerably better than non-buffered streams. With a regular InputStream you are making a read request for each byte, which is only going to increase the chance of a network issue if you have a crappy connection. Buffered Streams will read in the buffer amount of bytes per read (I think I set mine to 1024, but you could try different values to see what works best for you).

Hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消