Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using AFJSONRequestOperation

I'm trying to get some data from a web service serving JSON. But I have no idea what went wrong in my code. It looks so simple but I couldn't get any data.

This the code:

NSURLRequest *request = [NSURLRequest requestWithURL:URL];

AFJSONRequestOperation *operation = [AFJSONRequestOperation
   JSONRequestOperationWithRequest:request 
   success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
     DumpDic = (NSDictionary*)[JSON valueForKeyPath:@"description"] ;
   } 
   failure:nil
];

[operation start];

AboutTXT = [DumpDic objectForKey:@"description"];

Here is the JSON URL.

EDIT

JSON from the URL:

{
   "clazz":"AboutList",
   "description":{
      "clazz":"DescriptionContent",
      "description":"ASTRO Holdings Sdn. Bhd. (AHSB) Group operates through two holding companies – ASTRO Overseas Limited (AOL) which owns the portfolio of regional investments and ASTRO Malaysia Holdings Sdn Bhd (AMH / ASTRO) for the Malaysian business, which was privatized in 2010 and is currently owned by Usaha Tegas Sdn Bhd/its affiliates, and Khazanah Nasional Berhad."
   },
   "id":{
      "inc":-1096690569,
      "machine":1178249826,
      "new":false,
      "time":1339660115000,
      "timeSecond":1339660115
   },
   "refKey":"AboutList"
}
like image 424
danialmoghaddam Avatar asked Dec 13 '22 01:12

danialmoghaddam


1 Answers

Is it successfully connecting to the server, is the success block being called?

Fill in the failure block and NSLog the NSError returned by the failure block:

failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                                        NSLog(@"%@", [error userInfo]);
                                                    }

I also have one more tip, I would recommend building the NSURLRequest with AFNetwork's AFHTTPClient, it helps with various things and generally makes things simpler. You set the base URL and then give it a path to append to that base. Something like this:

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:address];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
NSMutableURLRequest *jsonRequest = [httpClient requestWithMethod:@"POST"
                                                            path:@"events"
                                                      parameters:dict];

Also instead of using valueForKeyPath may I suggest you just do a objectForKey:

[JSON objectForKey:@"description"];

Furthermore, you should not access DumpDic there:

[operation start];
AboutTXT = [DumpDic objectForKey:@"description"];

It's an async call, so once the operation has begun DumpDic will most likely be accessed before it is ever assigned the data from the server. So you are accessing a key that probably does not exist yet.

This should be done in the success or failure block. Those blocks are called once the connection has finished and the data is ready to be used.

So it should look more like this:

AFJSONRequestOperation *operation =
  [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                  success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    DumpDic = [JSON objectFor:@"description"];
    AboutTXT = [DumpDic objectForKey:@"description"];
  }
  failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"%@", [error userInfo]);
  }];

[operation start];
like image 85
Michael Boselowitz Avatar answered Dec 14 '22 23:12

Michael Boselowitz