IOS_POST _ network request

Source: Internet
Author: User

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.}];}

 

Related Article

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.