3

I have the following code which is pretty standard for fetching an xml document from the web in Android (from what I understand):

URL rssUrl = new URL("web_1.whatever.com");            
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
myXMLReader.parse(new InputSource(rssUrl.openStream()));

This works perfectly for most situations. The issue arises when I try to fetch an XML document from a web address that has an underscore, "_", in a subdomain, As in: web_1.whatever.com. openStream() apparently does not work with URLs that have an underscore in them. I for the life of me have not found any documentation on this and would like to find a way around this to make it work for URLs with an underscore. The URL that is entered comes from the user who is hosting the XML file and making them host the XML file on another domain is something I don't want to have to make them do. I have tried everything that I can think of, so any help would be immensely appreciated.

The error I get is below.

03-27 03:44:54.274: WARN/System.err(1051): java.io.IOException: Illegal character in host name at index 0: web_1.whatever.com

03-27 03:44:54.274: WARN/System.err(1051): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:874)

03-27 03:44:54.274: WARN/System.err(1051): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1152)

03-27 03:44:54.274: WARN/System.err(1051): at java.net.URL.openStream(URL.java:653)

Any ideas? Or is there any place I could get a look at the openStream code?

Thanks.

Community
  • 1
  • 1
Tom
  • 31
  • 1
  • 5

2 Answers2

4

Valid host names can not contain an underscore ("_") so what you see is correct behavior.

After some more searching I found the MS hostnames can violate the standard. The only option I can think of is to find a DNS resolver that can handle an underscore in the hostname and use the IP address directly.

Andrew White
  • 52,720
  • 19
  • 113
  • 137
  • 1
    Well I was reading this stack exchange [here](http://stackoverflow.com/questions/2180465/can-someone-have-a-subdomain-with-an-underscore-in-it) and came away with it thinking it was allowed for domain names. Maybe I am just not understanding that totally. Anyways, I really wanted to know if there was a work around. – Tom Mar 27 '11 at 01:58
  • A quick Google search will turn up all kinds of fuss on underscores in hostnames. The solutions seem to be library dependent which makes me stick by my pseudo answer (sorry for not a full solution) of find an alternative DNS resolver than can handle this violation. – Andrew White Mar 27 '11 at 02:00
0

you shoudle use :

InputStream in = Url.openConnection().getInputStream();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
RssHandler handler = new RssHandler();
parser.parse(in, handler);
Nanne
  • 64,065
  • 16
  • 119
  • 163
  • Still throws an error when using a subdomain with an underscore in it. Works for everything else though. Thanks for the suggestion. – Tom Apr 04 '11 at 03:15