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