无法读取 XML 文件
接收 xml 文件的内容(使用 TouchXML)时遇到一些问题。
我的第一个查询运行得很好,看起来像这样:
NSString *url = [NSString stringWithFormat:STATION_ID, latitude, longtitude];
CXMLDocument *rssParser = [[CXMLDocument alloc] initWithContentsOfURL:[NSURL URLWithString:url] options:0 error:nil];
NSLog(@"%@",rssParser);
所以这个日志给了我完整的 XML 文件。
之后,在另一种方法中,我尝试相同的方法:
NSString *url = [NSString stringWithFormat:WEATHER_URL, [positionInformation objectForKey:@"stationId"]];
CXMLDocument *rssParser = [[CXMLDocument alloc] initWithContentsOfURL:[NSURL URLWithString:url] options:0 error:nil];
NSLog(@"%@", rssParser);
但我得到的日志是(空)。 登录 URL 字符串为我提供了正确的 URL,不带空格或其他内容,例如 URL 日志如下所示:
NSString *url = [NSString stringWithFormat:WEATHER_URL, [positionInformation objectForKey:@"stationId"]];
NSLog(@"%@", url);
调试器控制台中的结果是
http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=KCASUNNY19
使用浏览器查看此文件似乎没问题。有谁知道怎么回事吗????
我也尝试过,首先通过 stringWithContentsOfURL 获取此 URL 的内容,但这也不起作用。
got a little problem with receiving the contents of a xml file (using TouchXML).
My first query works very well, looks something like this:
NSString *url = [NSString stringWithFormat:STATION_ID, latitude, longtitude];
CXMLDocument *rssParser = [[CXMLDocument alloc] initWithContentsOfURL:[NSURL URLWithString:url] options:0 error:nil];
NSLog(@"%@",rssParser);
so this log gives me the complete XML file.
after that, in another method im trying the same:
NSString *url = [NSString stringWithFormat:WEATHER_URL, [positionInformation objectForKey:@"stationId"]];
CXMLDocument *rssParser = [[CXMLDocument alloc] initWithContentsOfURL:[NSURL URLWithString:url] options:0 error:nil];
NSLog(@"%@", rssParser);
but the log im getting is (null).
Loggin the URL String gives me the right URL without spaces or something, the URL Log for example looks like this:
NSString *url = [NSString stringWithFormat:WEATHER_URL, [positionInformation objectForKey:@"stationId"]];
NSLog(@"%@", url);
The result in the debugger Console is
http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=KCASUNNY19
looking at this file with my browser seems to be ok. Anybody know whats going wrong?????
I also tried, first to get the content of this URL by stringWithContentsOfURL, but this also not worked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这家伙也遇到了同样的问题: http:// www.iphonedevsdk.com/forum/iphone-sdk-development/15963-problem-initwithcontentsofurl.html。看起来 Weather Underground 的 API 正在等待用户代理,并在没有看到用户代理时返回 500 HTTP 响应。您可以在 CXMLDocument 上使用
-initWithData:options:error:
来传递使用 NSMutableURLRequest 获取的数据。This guy was having the same problem: http://www.iphonedevsdk.com/forum/iphone-sdk-development/15963-problem-initwithcontentsofurl.html. Looks like the API at Weather Underground is expecting a user-agent and returning a 500 HTTP response when it doesn't see one. You can use
-initWithData:options:error:
on CXMLDocument to pass the data you get using NSMutableURLRequest.您应该尝试使用 initWithContentsOfURL:options:error: 的错误参数。创建一个 NSerror *对象,然后将其传入:
如果没有获得 rssParser 对象,请检查该错误。
You should try using the error argument of initWithContentsOfURL:options:error:. Create an NSerror *object, and pass it in thusly:
If you don't get an rssParser object, check that error.
我将此 xml 文件的内容复制到我自己的服务器并更改了 url 路径。
这工作正常,所以我认为读取这个“.asp”文件有问题。
唔。但它的 XML 内容很奇怪。
//编辑:
我在我的服务器上创建了一个小 php 文件,它返回“asp”文件的内容:
读取脚本的内容是有效的,但这种解决方法不是我想要的。
哦该死=/
i copied the content of this xml file to my own server and changed the url path.
this works correcty, so i think there is a problem with reading this ".asp" file.
hmm. but its XML content, strange.
//edit:
i made a little php file on my server that returns the content of the "asp" file:
reading the contents of my script works, but this workaround is not what i want.
oh damn =/
您确定 URL 字符串已正确编码,以便问号被解释为查询吗?在使用
dataUsingEncoding
或stringUsingEncoding
初始化rssReader
之前,您可能需要在 URL 上使用它。Are you sure that your URL string is properly encoded, so that the question mark gets interpreted as a query? You might want to look at using
dataUsingEncoding
orstringUsingEncoding
on your URL before using it to initializerssReader
.