IOS_POST _ network request
Synchronous post request
# Pragma mark-synchronous post request-(IBAction) POSTSynButtonDidClicked :( UIButton *) sender {// 1. concatenate baseUrlString NSString * baseUrlString = @ brief // 2. Create a URL Based on the string (Unified Resource Locator) NSURL * url = [NSURL URLWithString: baseUrlString]; // 3. Create a variable request object NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; // 1> set the request type to POST (GET by default) [request setHTTPMethod: @ POST]; // 2> you need to set the POST parameter NSString * bodyString = @ date = 20131129 & startRecord = 1 & len = 5 & udid = 1234567890 & terminalType = Iphone & cid = 213; // convert the string to NSData * bodyData = [bodyString dataUsingEncoding: NSUTF8StringEncoding]; [request setHTTPBody: bodyData]; // 4. request data (synchronous) NSData * receiveData = [NSURLConnection sendSynchronousRequest: request failed: nil error: nil]; // 5. NSDictionary * dict = [NSJSONSerialization failed: receiveData options :( response) error: nil]; NSArray * array = dict [@ news]; self. newsArray = [NSMutableArray array]; for (NSDictionary * smallDict in array) {NewsModal * modal = [[NewsModal alloc] init]; [modal convert: smallDict]; [self. newsArray addObject: modal];} for (NewsModal * modal in self. newsArray) {NSLog (% @, modal. title );}}
Asynchronous post request
# Pragma mark-asynchronous post request-(IBAction) POSTAsynButtonDidClicked :( UIButton *) sender {// 1. concatenate baseUrlString NSString * baseUrlString = @ brief // 2. Create a url Based on the string (Unified Resource Locator) NSURL * url = [NSURL URLWithString: baseUrlString]; // 3. Create a variable request object NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; // set relevant properties of the request object // 1> set the default request type to POST [request setHTTPMethod: @ POST]; // 2> set the POST parameter NSString * bodyString = @ date = 20131129 & startRecord = 1 & len = 5 & udid = 1234567890 & terminalType = Iphone & cid = 213; // 3> convert the string to NSData * bodyData = [bodyString dataUsingEncoding: Required]; [request setHTTPBody: bodyData]; // 4. request data (asynchronous) [NSURLConnection sendAsynchronousRequest: request queue: [inclumainqueue] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {// 5. NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: data options :( NSJSONReadingMutableContainers) error: nil]; NSArray * array = dict [@ news]; self. newsArray = [NSMutableArray array]; for (NSDictionary * smallDict in array) {NewsModal * modal = [[NewsModal alloc] init]; [modal convert: smallDict]; [self. newsArray addObject: modal];} for (NewsModal * modal in self. newsArray) {NSLog (% @, modal. title);} // reloadData the tableView; otherwise, the content is not displayed for the first time.}];}