Android weird URL fetching

386 Views Asked by At

I'm having a strange problem here. Here's the code, I'm using to fetch a url content:

URL u = new URL(url);
InputStream is = new BufferedInputStream(u.openStream());

I've got two urls, I want to fetch with this code. Both contain xml data. To be specific, the first one is http://www.berlingske.dk/unwire/latest/news_article/2/10, the second one is http://www.bt.dk/mecommobile/latest/news_article/1368/10?output_type=xml. The first one gets fetched correctly, the second one does not. I added some logging, and found out, that for the second url some weird html page gets fetched, instead of the expected xml. How can that be even possible?

1

There are 1 best solutions below

0
On BEST ANSWER

I think you're talking about URL redirects, which was a problem I was having. Try the following code:

URL url = new URL(url);
HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
ucon.setInstanceFollowRedirects(false);
URL secondURL = new URL(ucon.getHeaderField("Location"));
URLConnection conn = secondURL.openConnection();
InputStream is = new BufferedInputStream(conn.openStream());

The "magic" here happens in these 2 steps:

ucon.setInstanceFollowRedirects(false);
URL secondURL = new URL(ucon.getHeaderField("Location"));

By default InstanceFollowRedirects are set to true, but you want to set it to false to capture the second url. To be able to get that second url from the "weird html page", you need to get the header field called "Location".

Unless i misunderstood your problem, I hope this helps!