Trouble fetching "courses.json" over SSL


#1

Hi everyone, I can’t get the “courses” JSON object as I said in the subject… I used the error parameter from the JSONObjectWithData method, and I got a couple of messages depending of the options I set:

1st way:

[code] NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&error];
self.courses = jsonObject[@“courses”];

                                                     if (!self.courses) {
                                                         NSLog(@"JSON ERROR!! ----> %@", [error description]);
                                                     }[/code]

Error message ----> Error Domain=NSCocoaErrorDomain Code=3840 “The operation couldn’t be completed. (Cocoa error 3840.)” (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8e4b9a0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

2nd way

[code]NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error];
self.courses = jsonObject[@“courses”];

                                                     if (!self.courses) {
                                                         NSLog(@"JSON ERROR!! ----> %@", [error description]);
                                                     }

[/code]

Error message ----> Error Domain=NSCocoaErrorDomain Code=3840 “The operation couldn’t be completed. (Cocoa error 3840.)” (Invalid value around character 0.) UserInfo=0x8d3e430 {NSDebugDescription=Invalid value around character 0.}

What could be wrong? I manually checked the response from the secure web service using Chrome, and it seems to be ok (the same exactly JSON with no credentials… so?


#2

I got the same error requesting the JSON file over HTTPS and found the error comparing with the code from the book. I forgot the task: argument in the method

- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler { NSURLCredential *cred = [NSURLCredential credentialWithUser:@"BigNerdRanch" password:@"AchieveNerdvana" persistence:NSURLCredentialPersistenceForSession]; completionHandler(NSURLSessionAuthChallengeUseCredential, cred); }