SoFunction
Updated on 2025-04-09

Detailed explanation of iOS submitting JSON data through ASIHTTPRequest

Prior knowledge – What is ASIHTTPRequest?

Using the HTTP network request API in the iOS SDK is quite complicated and the calls are very cumbersome. ASIHTTPRequest is a set of APIs that encapsulate the CFNetwork API and is very simple to use. It is written in Objective-C and can be used well in applications on the Mac OS X system and iOS platform. ASIHTTPRequest is suitable for basic HTTP requests and interactions between REST-based services.

Upload JSON format data

First, give the main function code segment, and then analyze the code in detail:

NSDictionary *user = [[NSDictionary alloc] initWithObjectsAndKeys:@"0", @"Version", nil]; 
        if ([NSJSONSerialization isValidJSONObject:user]) 
        { 
          NSError *error; 
          NSData *jsonData = [NSJSONSerialization dataWithJSONObject:user options:NSJSONWritingPrettyPrinted error: &error]; 
          NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData]; 
          //NSLog(@"Register JSON:%@",[[NSString alloc] initWithData:tempJsonData encoding:NSUTF8StringEncoding]); 
           
          NSURL *url = [NSURL URLWithString:@"http://42.96.140.61/lev_version.php"]; 
          ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
          [request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"]; 
          [request addRequestHeader:@"Accept" value:@"application/json"]; 
          [request setRequestMethod:@"POST"]; 
          [request setPostBody:tempJsonData]; 
          [request startSynchronous]; 
          NSError *error1 = [request error]; 
          if (!error1) { 
            NSString *response = [request responseString]; 
            NSLog(@"Test:%@",response); 
          } 
        } 

The first line of the code segment:

NSDictionary *user = [[NSDictionary alloc] initWithObjectsAndKeys:@"0", @"Version", nil]; 

Constructs the simplest dictionary type data, because since iOS 5, it provides an API to convert NSDictionary to JSON format.

The second line if determines whether the dictionary data can be JSONized.

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

This sentence is a method to convert NSDictionary to JSON format. The data in JSON format is stored in NSData type variables.

NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData]; 

This sentence converts NSData to NSMutableData. The reason is that when we use ASIHTTPRequest to send JSON data, the message body must be stored in the format of NSMutableData.

The following sentence is looked away

//NSLog(@"Register JSON:%@",[[NSString alloc] initWithData:tempJsonData encoding:NSUTF8StringEncoding]); 

The main function is to record the data formatted by JSON just now

The following is the ASIHTTPRequest function section:

NSURL *url = [NSURL URLWithString:@"http://xxxx"]; 
          ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 

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

Next two sentences:

[request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"]; 
          [request addRequestHeader:@"Accept" value:@"application/json"]; 

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

Next is to set the request method (default is GET) and message body:

[request setRequestMethod:@"POST"]; 
          [request setPostBody:tempJsonData]; 

After all settings are completed, the synchronization request is enabled:

[request startSynchronous]; 

The last paragraph:

if (!error1) { 
            NSString *response = [request responseString]; 
            NSLog(@"Rev:%@",response); 
          } 

It is the response information returned by the print server.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.