I have a webmethod wich returns a lot of json data.
I have a separate class where I fetch my data. In this class I do the following.
+ (NSDictionary *)executeGenkFetch:(NSString *)query
{
query = [NSString stringWithFormat:@"%@&format=json&nojsoncallback=1", query];
query = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// NSLog(@"[%@ %@] sent %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), query);
NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error] : nil;
if (error) NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription);
// NSLog(@"[%@ %@] received %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), results);
return results;
}
+ (NSArray *)GetNieuws
{
NSString *request = [NSString stringWithFormat:@"http://www.krcTest.be/mobile/json/request/webmethod"];
return [[self executeGenkFetch:request] valueForKeyPath:@"news.title"];
}
Now I have a button to do a NSLog with the data. Here you see the action behind the button.
- (IBAction)testFetch:(id)sender {
NSArray *nieuws;
nieuws = [GenkData GetNieuws];
NSLog(@"%@",nieuws);
}
In my Log always shows up this.
2012-10-01 10:52:16.379 RacingGenk[13434:c07] (null)
Could anybody help me?
Your endpoint gives a DNS error which may explain why you have no data, however assuming you might have anonomised it there are plenty of ways to debug this yourself.
Start by not chaining method calls together like this;
Instead prefer (and note in particular you were missing the error response from one call the way you were doing it);
By not chaining your method calls together, you can now print intermediate results to see what the problem is, and you can also use breakpoints to step through it much better too.
While I’m here, it’s odd to have methods like this as class methods rather than instance methods and you should be aware that synchronous network use like this will kill your UI performance unless you are doing it on another thread which I suspect you are not. You may wish to look at the asynchronous network methods of NSURLConnection instead.
I haven’t syntax checked the above as I’m typing this on a PC, but it should point you in the right direction.