views:

329

answers:

1

Hello everyone, what are the best practices 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.

+1  A: 

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.

Nemi
Thanks Nemi. I currently have the InputStream wrapped in a BufferedInputStream before passing it to the parser.
Jeffrey