iOS submits JSON data via ASIHTTPRequest

Source: Internet
Author: User

Prior knowledge-what is ASIHTTPRequest?

Using the HTTP Network request API in the iOS SDK is quite complicated and cumbersome to call, ASIHTTPRequest is a set of APIs that encapsulates the Cfnetwork API and is very simple to use, written in Objective-c, can be very well applied in Mac OS x systems and iOS platform applications. ASIHTTPRequest applies to basic HTTP requests, and to the interaction between rest-based services.

How do I use ASIHTTPRequest?

There are many articles on the internet dedicated to the use of asihttprequest, very detailed, the landlord will not repeat the HA, here gives a classic introduction of the detailed article link: http://www.cnblogs.com/dotey/archive/2011/05/10/2041966.html

Uploading JSON-formatted data

First, the main function code snippet is given, and then the code is parsed in detail:

[CPP]View Plaincopy
  1. Nsdictionary *user = [[Nsdictionary alloc] initwithobjectsandkeys:@"0" @"Version", nil];
  2. if ([nsjsonserialization Isvalidjsonobject:user])
  3. {
  4. Nserror *error;
  5. NSData *jsondata = [nsjsonserialization datawithjsonobject:user options:nsjsonwritingprettyprinted error: &error] ;
  6. Nsmutabledata *tempjsondata = [Nsmutabledata datawithdata:jsondata];
  7. //nslog (@ "Register json:%@", [[NSString alloc] Initwithdata:tempjsondata encoding:nsutf8stringencoding]);
  8. Nsurl *url = [Nsurl urlwithstring:@"http://42.96.140.61/lev_version.php"];
  9. ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];
  10. [Request addrequestheader:@"Content-type" value:@ "Application/json;  Encoding=utf-8 "];
  11. [Request addrequestheader:@"Accept" value:@"Application/json"];
  12. [Request setrequestmethod:@"POST"];
  13. [Request Setpostbody:tempjsondata];
  14. [Request startsynchronous];
  15. Nserror *error1 = [request ERROR];
  16. if (!error1) {
  17. NSString *response = [request responsestring];
  18. NSLog (@"test:%@", response);
  19. }
  20. }

The first line of code snippet:

[CPP]View Plaincopy
    1. Nsdictionary *user = [[Nsdictionary alloc] initwithobjectsandkeys:@"0" @"Version", nil];

Constructs one of the simplest dictionary-type data, since iOS 5 provides an API to convert nsdictionary into JSON format.

The second line if it determines if the dictionary data can be JSON.

[CPP]View Plaincopy
    1. NSData *jsondata = [nsjsonserialization datawithjsonobject:user options:nsjsonwritingprettyprinted error: &error] ;

This is the way to convert nsdictionary into JSON format, where JSON-formatted data is stored in variables of type NSData.

[CPP]View Plaincopy
    1. Nsmutabledata *tempjsondata = [Nsmutabledata datawithdata:jsondata];

This sentence is to convert the NSData into Nsmutabledata, because the following we want to use ASIHTTPRequest to send JSON data, its message body must be stored in the Nsmutabledata format.

The following sentence to watch out

[CPP]View Plaincopy
    1. NSLog (@ "Register json:%@", [[NSString alloc] Initwithdata:tempjsondata encoding:nsutf8stringencoding]);

The main function is to record the data that was just JSON formatted

Below is the ASIHTTPRequest functional section:

[CPP]View Plaincopy
    1. Nsurl *url = [Nsurl urlwithstring:@"http://xxxx"];
    2. ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];

The main function of these two sentences is to set the server-side address to interact with the client.

The next two sentences:

[CPP]View Plaincopy
    1. [Request addrequestheader:@"Content-type" value:@ "Application/json;  Encoding=utf-8 "];
    2. [Request addrequestheader:@"Accept" value:@"Application/json"];

is the header information that sets the HTTP request information, from which you can see that the content type is JSON.

The next step is to set the request method (default to get) and the message body:

[CPP]View Plaincopy
    1. [Request setrequestmethod:@"POST"];
    2. [Request Setpostbody:tempjsondata];

After everything is set up, turn on the sync request:

[CPP]View Plaincopy
    1. [Request startsynchronous];


The last paragraph:

[CPP]View Plaincopy
    1. if (!error1) {
    2. NSString *response = [request responsestring];
    3. NSLog (@"rev:%@", response);
    4. }

Is the response information returned by the print server.

iOS submits JSON data via ASIHTTPRequest

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.