0

My mission is to parse some XML from a website from Android. Everything works fine if I paste the XML to a String and then convert it to a InputStream and parse it. But if I try to download the XML using HttpURLConnection it only downloads the "half" and skips the root element and some attributes in the beginning and at the end.

The thing I want to parse looks like this:

<Page xmlns:i... xmlns="...">
<Body>...</Body>
<Contact i:nil=".."/>
<ID>...</ID>
<Date>...</Date>
<Summary>..</Summary>
<Title>...</Title>
</Page>

The thing I get when I download and print out:

<ID>...</ID>
<Date>...</Date>
<Summary>..</Summary>
<Title>...</Title

and hence the parse fails as it does not get what it expected. My code for downloading:

URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(25000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setUseCaches(true);
conn.connect();
InputStream stream = conn.getInputStream();
return stream;      

Why does it not download the whole XML page from the net when it really is reachable?

4

1 回答 1

0

如果我添加以下行,问题就解决了:

conn.setRequestProperty("Content-Type", " application/xml; charset=utf-8");

很高兴知道。

于 2013-06-11T19:56:53.657 回答