开发者

Using openStream with a URL that has a subdomain with an underscore in it returns ioexception in Android. Why?

开发者 https://www.devze.com 2023-02-20 13:47 出处:网络
I have the following code which is pretty standard for fetching an xml document from the web in Android (from what I understand):

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(U开发者_如何学GoRL.java:653)

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

Thanks.


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.


you shoudle use :

InputStream in = Url.openConnection().getInputStream();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
RssHandler handler = new RssHandler();
parser.parse(in, handler);
0

精彩评论

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