I am having some trouble with my app parsing the JSON from PHP. What I do, is echo (JSON_Encoded) an array on PHP and when I use the url, it returns the JSON format on the browser.
The problem is that when I try to parse it, the app crashes saying that the data is null.
This is the url I am using (PHP):
http://iamitman.com/interdev/ScanAPP/asist.php?EvtGN=3500&VistGN=500
And this is the code to parse it:
// Download JSON
NSData *JSONData = [NSData dataWithContentsOfURL:[NSURL URLWithString:theURL]];
NSLog(@"%@",JSONData);
// Parse JSON
NSArray *jsonResult = [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:nil];
self.data = jsonResult;
NSMutableArray *_names = [NSMutableArray array];
for (id item in jsonResult)
[_names addObject:[NSString stringWithFormat:@"%@ %@", item[@"Name"], item[@"Last"]]];
self.names = _names;
The app crashes with this error code:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
Yet, if I use this url (JSON format, txt), the code works fine:
https://raw.githubusercontent.com/VinceG/Auto-Cars-Makes-And-Models/master/models-json.txt
The difference is that is a .txt file in JSON format.
Why is the app crashing then? Any tips?
Thank you!