3

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.

choise
  • 24,636
  • 19
  • 75
  • 131

4 Answers4

2

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.

Don
  • 3,654
  • 1
  • 26
  • 47
  • amazing. thats it! sadly i'm now using an other API for my weather informationn :( but next time :D – choise Feb 03 '10 at 11:17
0

You should try using the error argument of initWithContentsOfURL:options:error:. Create an NSerror *object, and pass it in thusly:

NSError      *error;
CXMLDocument *rssParser = [[CXMLDocument alloc] initWithContentsOfURL:[NSURL URLWithString:url] options:0 error: &error];

If you don't get an rssParser object, check that error.

Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
  • hmm the error object is: "Error : Operation could not be completed. (Cocoa error 256.)" – choise Jan 23 '10 at 13:36
  • do you mean [error userInfo] ? in there is: "{ NSURL = http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=KCASUNNY19; }" – choise Jan 23 '10 at 13:50
0

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:

<?php
  echo file_get_contents("http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=KCASANFR70");
 ?>

reading the contents of my script works, but this workaround is not what i want.

oh damn =/

choise
  • 24,636
  • 19
  • 75
  • 131
0

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 or stringUsingEncoding on your URL before using it to initialize rssReader.

plasticsaber
  • 232
  • 1
  • 4
  • yeah id tried that a few times. first with an encoded url string (which had no effect, because in this url is nothing to encode) and then with a NSString contentfromurl using encoding (i tired ascii and utf8). – choise Jan 26 '10 at 07:41