joseph
joseph

Reputation: 25

About web service Json/rest api/Nsurl connection

I am starting learning about web service, where I use one url api to get data and to display in my table view. But I saw some tutorials - in that they use NSURLConnection or Rest API or AFNetworking.

I am really confused about all type. Which one should I use in that above type. For web service which type should I use. And also I saw some doubts in SO that use synchronous or asynchronous. Thus this any another type to get data from URL?

Actually for all web service, which should I use to get data and display?

Upvotes: 1

Views: 441

Answers (4)

Vijay Sanghavi
Vijay Sanghavi

Reputation: 340

NSURLConnection

This lets you to load the content of URL by providing the URL request object. By using NSURLConnection you can load URL requests both asynchronously using a callback block and synchronously. See this example

NSURL *URL = [NSURL URLWithString:@"http://example.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

[NSURLConnection sendAsynchronousRequest:request
                                    queue:[NSOperationQueue mainQueue]
                        completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
     // ...
 }];

For more you can go to apple docs

AFNetworking

This is third party library built on the top of Foundation URL Loading. This is very easy to install through pods and handy to use. See below example like how I am using the same in my app

-(AFHTTPRequestOperationManager *)manager
{
    if (!_manager)
    {
        _manager = [AFHTTPRequestOperationManager manager];
        _manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    }   
    return _manager;
}

Above we are initializing the instance of AFHTTPRequestOperationManager *manager

[self.manager POST:@"http://example.com" parameters:parameters

               success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSError *error;
         NSMutableDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
         // return response dictionary in success block
     }

               failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         // return error in failure block
     }]

Above method will load data asynchronously and remaining is self explanatory. But if you want to block the user interface like a synchronous request than use [operation waitUntilFinished] which is an anti-pattern. Here operation is a instance of AFJSONRequestOperation.

Upvotes: 1

Tj3n
Tj3n

Reputation: 9923

NSURLConnection and NSURLSession is apple API use to manage network operation like download and upload, AFNetworking is the framework that use those 2 API and added multithreading/error handling/network reachability....to make your life easier, RESTful is the architecture for client-server connecting, u can implement it in your serverside to return things back to your clientside in easy to use model (JSON).

synchronous mean u wait for it to complete to do anything else, asynchronous means u just start it but don't need to wait for it, like u do a request to server and user still can interact with your UI at the same time, so its advised that use asynchronous task to request to server then only update the UI in synchronous

hope my explain is easy to understand and correct :)

Upvotes: 1

Om Patel
Om Patel

Reputation: 151

Firstly, AFNetworking and NSURLConnection are used on Mobile Side. Rest API is not from mobile side. Rest API you implemented on server side which handle CRUD operations like GET, POST, PUT and DELETE.

Third party libraries are there to ease our work. And AFNetworking is very popular and trustworthy library.

AFNetworking makes asynchronous network requests. To read more about it, visit Introduction to AFNetworking.

AFNetworking does everything NSURLConnection can. Using it now will save you a lot of time writing boilerplate code!

Upvotes: 2

Nikunj5294
Nikunj5294

Reputation: 391

 -(void)JsonDataParsing
 {


    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager POST:url parameters:params
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
              NSDictionary *jsonDict = (NSDictionary *) responseObject;
              //!!! here is answer (parsed from mapped JSON: {"result":"STRING"}) ->
              NSString *res = [NSString stringWithFormat:@"%@", [jsonDict objectForKey:@"result"]];
          } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              //....
          }
     ];

 }

Upvotes: 2

Related Questions