AFNetworking and jSON

后端 未结 7 949
臣服心动
臣服心动 2020-12-28 08:57

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.



        
相关标签:
7条回答
  • 2020-12-28 09:30

    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);
    }];
    
    0 讨论(0)
  • 2020-12-28 09:30

    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

    0 讨论(0)
  • 2020-12-28 09:31
    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'

    0 讨论(0)
  • 2020-12-28 09:37

    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.

    0 讨论(0)
  • 2020-12-28 09:38

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

    0 讨论(0)
  • 2020-12-28 09:41

    First, in order to run AFNetworking you will first need to install “CocoaPods“. What is this? CocoaPods is a dependency manager for for Objective-C which makes installing third party libraries as AFNetworking much more faster and secure. It is provided as a Ruby Gem which you can install the following way:

    Open up the terminal and execute the following commands:

    sudo gem install cocoapods -V
    

    Be patient. This command can take some time to finish, so the “-V” option will provide you with detailed data what’s going on.

    Next we execute:

    pod setup
    

    Which sets up our CocoaPods installation.

    Now we have to create a PodFile which will server as our config file for CocoaPods.

    Back to the terminal and we write the following commands:

    touch Podfile
    open -e Podfile
    

    The first command will create a file named “Podfile” with the Unix touch editor. The second line will open this newly created file for editing. By now, you should see an empty TextEdit window opened. Now we paste the following inside this file:

    platform :ios, '7.0'
    pod "AFNetworking", "~> 2.0"
    

    Now it is time to set up our Cocoa working environment. IMPORTANT: Please make sure to change your directory with your terminal at your XCODE PROJECT’S CURRENT DIRECTORY by using the ‘cd’ command in the terminal. Example:

    cd /user/xcode/myproject
    

    Once you are sure that you are at the proper directory execute the following terminal command:

    pod setup
    

    And then we install the dependencies to our project by writing:

    pod install
    

    Now, this is an important note: Since you are trying to install a project which was previously not inside your current Xcode project you have to create a Workspace for your current Xcode project (if you have not done this already.) To create a workspace for your Xcode project do the following:

    File > Save Workspace
    

    Once you are done, navigate to the directory of your Xcode workspace file and execute the following command:

    open YourProjectName.xcworkspace
    

    Now you can just use “import ” at the beginning of any of your files to access the builtin API functionalities of AFNetworking.

    0 讨论(0)
提交回复
热议问题