RSS Feed

AFNetworking : How to post data (NSData) on server.

Posted on

Few of my friends are facing issue in uploading JSON data on server. I am writing this post for those developer who are new in AFNetworking and unable to upload JSON data on there web server.

1. Create your parameter dictionary as : 
 NSDictionary *jsonDic = [NSDictionary dictionaryWithObjectsAndKeys

 value,         @”key”,

 value,         @”key”,

 value,         @”key”, // etc

                         nil];

2. Convert this dictionary into data : 

NSError *error = nil;

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonDic options:NSJSONWritingPrettyPrinted error:&error];

3. Now follow this to upload data on server :

// Create request body 

    NSMutableData *body = [NSMutableData data];

// Append of the body with your data

    [body appendData:jsonData];

        [request setHTTPBody:body];

// Set request method POST

     [request setHTTPMethod:@"POST"];

// Set request Headers

    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

    [request setValue:@"application/json" forHTTPHeaderField:@"content-type"];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

 // Success

  NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

              NSDictionary *responseDict = [responseString JSONValue];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

// Failure

        NSLog(@”Error: %@”, error);

    }];

      [operation start];

 

Cheers !!

About these ads

About Muzammil

Over the past 4 years, I have designed, developed and managed over 60 iPhone/iPad applications for many large-scale clients, some include Rolex, Ford, Makemark LLC, Source Digital Production, IT City (Kingdom of Saudi Arabia), R.Ø.S.A. Creation etc. These applications ranged from simple navigation bar application to high end user interface design for image processing, simple games with Core Graphics and OpenGL. Before started Triffort Technologies i was working in Ampere Software Pvt Ltd as a senior iOS Developer. I was the first employe of Ampere who started iPhone/iPad development in the company. In ampere i worked on some complex apps like Boxboss, Austin Post, User Tracking etc. Throughout my career, I have used Core Graphics, OpenGL, SQLite, Core Data, JSON, XML etc. I am very proficient in many design programs including Photoshop, Illustrator, Flash, iMove. Although I am very knowledgeable in all Adobe Products. Most the time i am designing my apps without taking help from any graphics designer. I love to make app from my designs. It feels great when you are creating app from scratch and no body else is involve in your ideas. You are free to do anything. As the Senior Developer and Team lead at Triffort Technologies, I managed a team of Technical Interactive Developers and Designers. As their manager I am responsible for scheduling, resource management, task reviews, process development and enhancements, employee reviews and new employee hiring. I believe good employee morale and communication are key factors to successful, creative work, therefore, positive feedback was part of my daily routine. But most of the time you can find me on my desk writing codes for some app. I love programming. This is my passion. I’m a team player. I am a motivated individual who enjoys taking on new challenges and learning new technologies. I am well organized and a proficient multi-tasker. I have strong problem solving skills such as the ability to use technology in creative ways to meet unique project requirements. I love to be challenged and I always have a positive outlook no matter how tight the deadline is. If you would like to contact me or find out more about my experience, please contact me at muzammil@triffort.com or you can Skype me at muz.iphonedev.

8 responses »

  1. request? What is it?

    Reply
    • Here request is your NSMutableURLRequest :

      NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
      path:urlString
      parameters:nil];

      *urlString is your web-service url.

      Reply
  2. Also code mostly isnt working for last version of AFNetworking

    Reply
  3. httpClient? what is it?

    Reply
  4. Thanks Muzammil! This is the unique tutorial with POST request and JSON 100% working to AFNetworking 1.x

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 68 other followers

%d bloggers like this: