0

I'm messing with some API stuff and tried the following:

#define searchWebService @"https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=b667841296224aab0371a6f4a4546662&format=json&per_page=20&page=1"

// Construct url string for search
NSString *urlString = [NSString stringWithFormat:@"%@&text=%@&nojsoncallback=1", searchWebService, keyword];
//NSString *formattedURLString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

// Create url via formatted string
NSURL *url = [NSURL URLWithString:urlString];

// Get all data from the return of the url
NSData *photoData = [NSData dataWithContentsOfURL:url];

// Place all data into a dictionary
NSDictionary *allData = [NSJSONSerialization JSONObjectWithData:photoData options:kNilOptions error:nil];

Here is the URL that is built: https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=b667841296224aab0371a6f4a4546662&format=json&per_page=20&page=1&text=ball&nojsoncallback=1

When I plug this URL into a web browser I get formatted JSON but when I try to plug that into:NSData *photoData = [NSData dataWithContentsOfURL:url];

I get 'data parameter is nil'.

Any ideas what I'm doing wrong?

UPDATE:

I'm now using:

// Construct url string for search
NSString *urlString = [NSString stringWithFormat:@"%@&text=%@&nojsoncallback=1", searchWebService, keyword];

NSString *formattedURLString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];

[NSURLConnection sendAsynchronousRequest:theRequest queue:nil completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    NSLog(@"BOOM %s %@", __func__, response);
    if(!connectionError){
}
    else{
        NSLog(@"%s %@", __func__, connectionError.localizedDescription);
    }

but neither of the logs ever show in the console.

UPDATE: TEST PROJECT I've created a brand new project and put the following code in the viewDidLoad method:

NSString *urlString = [NSString stringWithFormat:@"%@", webServiceGetGlobalScores];

NSString *formattedURLString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:formattedURLString]];
NSLog(@"theRequest: %@", theRequest);
[NSURLConnection sendAsynchronousRequest:theRequest queue:nil completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    NSLog(@"BOOM %s %@", __func__, response);
    if(!connectionError){
    }
    else{
        NSLog(@"%s %@", __func__, connectionError.localizedDescription);
    }
}];

And this is the defined #define webServiceGetGlobalScores @"http://www.appguys.biz/JSON/iTapperJSON.php?key=weBeTappin&method=getGlobalScores"

But still the sendAsynchronousRequest does not log anything.

UPDATE 3

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Construct url string for search
NSString *urlString = [NSString stringWithFormat:@"%@", webServiceGetGlobalScores];
NSURL *url = [NSURL URLWithString:urlString];

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSLog(@"urlRequest: %@", urlRequest);

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {NSLog(@"BOOM %s %@", __func__, response);
     NSLog(@"ERROR: %@", error);
    if ([data length] > 0 && error == nil){
        NSLog(@"BOOM");
    }
}];
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
bnjmn.myers
  • 409
  • 2
  • 6
  • 15
  • Use asynchronous downloading data NSData:dataWithContentsOfURL: isn't good way. – Tirth Feb 02 '15 at 04:58
  • what NSLog(@"BOOM %s %@", __func__, response); this line returning you data? – Tirth Feb 02 '15 at 05:52
  • I never see that line logged. It's like it's not getting into that block at all. – bnjmn.myers Feb 02 '15 at 05:55
  • In the test project that I newly created I get this: BOOM __29-[ViewController viewDidLoad]_block_invoke (null) – bnjmn.myers Feb 02 '15 at 05:57
  • if I log error I get the following: ERROR: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0x7fab23828e30 {NSUnderlyingError=0x7fab22725cb0 "The network connection was lost.", NSErrorFailingURLStringKey=http://www.appguys.biz/JSON/iTapperJSON.php?key=weBeTappin&method=getGlobalScores, NSErrorFailingURLKey=http://www.appguys.biz/JSON/iTapperJSON.php?key=weBeTappin&method=getGlobalScores, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=57, NSLocalizedDescription=The network connection was lost.} – bnjmn.myers Feb 02 '15 at 06:02
  • What is odd is that I can plug that url into the browser and I get a JSON return. – bnjmn.myers Feb 02 '15 at 06:04
  • Reset your simulator or device and check it out. Newer version of iOS simulator having issue. I'm facing too some days ago. – Tirth Feb 02 '15 at 06:09
  • 2
    see issue http://stackoverflow.com/questions/25797339/nsurlconnection-get-request-returns-1005-the-network-connection-was-lost – Tirth Feb 02 '15 at 06:12
  • Tirth!!!! I LOVE YOU! This was EXACTLY the problem. Stupid simulator! Grrrrrrrrrrrrr. Thanks! – bnjmn.myers Feb 02 '15 at 06:14

2 Answers2

0

You can use this code for downloading data,

NSString *urlString = //your whatever URL
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];

    [NSURLConnection sendAsynchronousRequest:theRequest queue:nil completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    NSLog(@"%s %@", __func__, response);
        if(!connectionError){
            //Parse your JSON data
        }
        else{
            NSLog(@"%s %@", __func__, connectionError.localizedDescription);
        }
    }];

Edited Other way works!!! o_O see following Now i surprised why following code working instead of above.

NSString *urlString = @"https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=b667841296224aab0371a6f4a4546662&format=json&per_page=20&page=1&text=ball&nojsoncallback=1";

NSString *formattedURLString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSError *theErr;
NSData *thd = [NSData dataWithContentsOfURL:[NSURL URLWithString:formattedURLString] options:0 error:&theErr];
if(theErr){
    NSLog(@"%@", theErr.localizedDescription);
}
else{
    NSLog(@"%@", [[NSString alloc] initWithData:thd encoding:NSUTF8StringEncoding]);
}

3rd Way, Newer iOS simulator version > 6.x having some issue. Reset your simulator and check it out your code. For reference go NSURLConnection GET request returns -1005, "the network connection was lost"

Community
  • 1
  • 1
Tirth
  • 7,801
  • 9
  • 55
  • 88
  • Thanks for the help. The urlRequest formats just fine but the sendAsynchronousRequest never seems to be firing. I've put a log statement in it as well as in the "if" and the "else" but the Log never shows up. Not sure what's going on. – bnjmn.myers Feb 02 '15 at 05:23
  • @bnjmn.myers Can you update your question with updated code?? see my updated answer i place another log reading information about response. Updated that response also in your question. – Tirth Feb 02 '15 at 05:31
  • I've updated now. Another interesting tidbit is that all of the apps that I have were built pre iOS 8 and Xcode 6 and now they are all throwing the same error any time that I try to retrieve JSON. Did something happen that introduced a major change to retrieving JSON? – bnjmn.myers Feb 02 '15 at 05:42
0

Try using + dataWithContentsOfURL:options:error: instead, which gives you an NSError object. Plus, notice that that the docs say:

Do not use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated.

Instead, for non-file URLs, consider using the dataTaskWithURL:completionHandler: method of the NSSession class. See URL Loading System Programming Guide for details.

André Fratelli
  • 5,920
  • 7
  • 46
  • 87