POST asynchronous request and iOSPOST asynchronous request in iOS

Source: Internet
Author: User

POST asynchronous request and iOSPOST asynchronous request in iOS

POST asynchronous request (proxy)

1. Follow <NSURLConnectionDataDelegate>

@interface ViewController ()<NSURLConnectionDataDelegate>

2. The NSMutableData type reData attribute is used to splice data.

@property (nonatomic,strong)NSMutableData *reDtata;

3. Get url

 NSString *urlString = @"http://api.tudou.com/v3/gw";    NSURL *url = [NSURL URLWithString:urlString];

4. Create a request

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

5. Set HTTPMethod to POST (GET request by default)

request.HTTPMethod = @"POST";

6. Set HTTPBody (the body part in the url, which must be converted if the body part contains Chinese characters)

 NSString *bodyStr = @"method=album.channel.get&appKey=myKey&format=json&channel=c&pageNo=1&pageSize=15";    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];    request.HTTPBody = bodyData;

7. Create a connection and set a proxy

  [NSURLConnection connectionWithRequest:request delegate:self];

8. Implement proxy Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    self.reDtata = [NSMutableData data];}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    [_reDtata appendData:data];}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:_reDtata options:(NSJSONReadingAllowFragments) error:nil];    NSLog(@"%@",dic);}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{    }

 

 

 

The following is all the implemented code

-(IBAction) postAsyc :( id) sender {} is the control code dragged out of the storyboard. You can also directly write the code implementation and write a button and Its Implementation Method.
#import "ViewController.h"@interface ViewController ()<NSURLConnectionDataDelegate>@property (nonatomic,strong)NSMutableData *reDtata;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}- (IBAction)postAsyc:(id)sender {    NSString *urlString = @"http://api.tudou.com/v3/gw";    NSURL *url = [NSURL URLWithString:urlString];    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    request.HTTPMethod = @"POST";    NSString *bodyStr = @"method=album.channel.get&appKey=myKey&format=json&channel=c&pageNo=1&pageSize=15";    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];    request.HTTPBody = bodyData;    [NSURLConnection connectionWithRequest:request delegate:self];    }- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    self.reDtata = [NSMutableData data];}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    [_reDtata appendData:data];}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:_reDtata options:(NSJSONReadingAllowFragments) error:nil];    NSLog(@"%@",dic);}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{    }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

 

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.