Using AFJSONRequestOperation

I am trying to get some data from a web service serving JSON. But I don’t know what went wrong in my code. It looks so simple, but I could not get any data.

This 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 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" } 
+4
source share
1 answer

Does it connect successfully to the server, is it called a success block?

Fill the block of failures and the NSLog NSError returned by the block of failures:

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

I have one more tip, I would recommend creating NSURLRequest with AFNetwork AFHTTPClient, it helps with various things and, as a rule, simplifies the work. You set the base URL and then give it a path to add to this base. Something like that:

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

Also, instead of using valueForKeyPath, I can suggest that you simply make objectForKey:

 [JSON objectForKey:@"description"]; 

Also, you should not access DumpDic:

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

This is an asynchronous call, so after the start of a DumpDic operation, it will most likely be available before it is assigned data from the server. This way you gain access to a key that probably doesn't exist yet.

This should be done in a block of success or failure. These blocks are called as soon as the connection is completed and the data is ready for use.

Therefore, it should look something 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]; 
+13
source

All Articles