AFNetworking and jSON

31,632

Solution 1

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"link"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request 
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        NSDictionary *jsonDict = (NSDictionary *) JSON;
        NSArray *products = [jsonDict objectForKey:@"products"];
        [products enumerateObjectsUsingBlock:^(id obj,NSUInteger idx, BOOL *stop){
            NSString *productIconUrl = [obj objectForKey:@"icon_url"];
        }];

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

[operation start];

Try this.

Update 1: You can try this https://github.com/SSamanta/SSRestClient

Update 2: https://github.com/SSamanta/SSHTTPClient (Using Swift)

Available Pod : pod 'SSHTTPClient', '~>1.2.2'

Solution 2

if you're using AFNetworking 3.0, AFJSONRequestOperation doesn't exist anymore, you have to use AFHTTPSessionManager :

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Solution 3

To parse JSON with AFNetworking, just create a subclass and add the following during initialization.

[self registerHTTPOperationClass:[AFJSONRequestOperation class]];

Then calling a method like GET:parameters:completion: will call the completion block with an NSDictionary as the response parameter (assuming the JSON is valid).

To download the images, assuming you want to display them, check out UIImageView+AFNetworking.

Solution 4

If you are just starting, I'd recommend using RestKit for this task (it makes use of AFNetworking). See an example here.

Solution 5

Please check the example of AFNetworking https://github.com/AFNetworking/AFNetworking/tree/master/Example

For image downloading, EGOCache & EGOImageLoading may be a good choice https://github.com/enormego/EGOImageLoading

Share:
31,632
fizampou
Author by

fizampou

¯_(ツ)_/¯

Updated on February 12, 2020

Comments

  • fizampou
    fizampou over 4 years

    I am getting following the json response from a web-service api. I want to extract product data from the json. I also want to implement this using AFNetworking.

     {"products": [
        {
          "product_id": "1170",
          "name": "zzzz®",
          "sort_order": 0,
          "brand": "zzzas",
          "product_category_id": "1090",
          "location_ids": [
            "1078"
          ],
          "icon_url": "http://zzzzz.com/media/2502/zzzz.png",
          "icon_date": "Wed, 07 Nov 2012 14:03:47 GMT",
          "thumbnail_url": "http://zzzz.com/media/2591/zzdfs.png",
          "thumbnail_date": "Wed, 07 Nov 2012 14:04:02 GMT"
        },
        {
          "product_id": "1126",
          "name": "ddddd®",
          "sort_order": 1,
          "brand": "dddsas",
          "product_category_id": "1110",
          "location_ids": [
            "1095"
          ],
          "icon_url": "http://zzzzz.com/media/2507/ddddd.png",
          "icon_date": "Wed, 07 Nov 2012 14:03:48 GMT",
          "thumbnail_url": "http://zzzzz.com/media/2596/sssds.png",
          "thumbnail_date": "Wed, 07 Nov 2012 14:04:05 GMT"
        }
    ]}
    

    Can anyone suggest a way to do this.me how the things will be done.

  • fizampou
    fizampou over 11 years
    i also want to download some really large pdf files and some videos. there i think i had a problem with those
  • fizampou
    fizampou over 11 years
    thanks a lot that worked for parsing the file also check this out if you have some spare time stackoverflow.com/questions/13478437/…
  • fizampou
    fizampou over 11 years
    i think rest kit is too complex for my application
  • Susim Samanta
    Susim Samanta over 11 years
    @Filip check the answer in that link
  • jianpx
    jianpx almost 11 years
    only registerhttpoperationclass is not enough to get the json response data. The fully steps are : [self registerHTTPOperationClass:[AFJSONRequestOperation class]]; and [self setDefaultHeader:@"Accept" value:@"application/json"] . refers to here
  • tybro0103
    tybro0103 almost 11 years
    RestKit does seem rather bloated now.
  • Michael Mior
    Michael Mior over 10 years
    @jianpx This depends on the server you're talking to and whether or not you want JSON responses for all your requests. If you do, then definitely set the header.