标签:
1、iOS SDK 同步GET请求
NSString *strURL = [[NSString alloc] initWithFormat:@"http://www.51work6.com/service/mynotes/WebService.php?email=gs.654586026@qq.com&type=JSON&action=query"];
strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strURL];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"请求完成...");
NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"resDict:%@",resDict);
2、iOS SDK 异步GET请求
/*
* 开始请求Web Service
*/
-(void)startRequest
{
NSString *strURL = [[NSString alloc] initWithFormat:@"http://www.weather.com.cn/adat/sk/101010100.html"];
strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strURL];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self];
if (connection) {
self.datas = [NSMutableData new];
}
}
#pragma mark- NSURLConnection 回调方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.datas appendData:data];
}
-(void) connection:(NSURLConnection *)connection didFailWithError: (NSError *)error {
NSLog(@"%@",[error localizedDescription]);
}
- (void) connectionDidFinishLoading: (NSURLConnection*) connection {
NSLog(@"请求完成...");
NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:self.datas options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dict:%@",dict);
}
iOS SDK POST请求
/*
* 开始请求Web Service
*/
-(void)startRequest
{
NSString *strURL = @"http://www.51work6.com/service/mynotes/WebService.php";
strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strURL];
NSString *post = [NSString stringWithFormat:@"email=%@&type=%@&action=%@", @"gs.654586026@qq.com",@"JSON",@"query"];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSURLConnection *connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self];
if (connection) {
self.datas = [NSMutableData new];
}
}
#pragma mark- NSURLConnection 回调方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.datas appendData:data];
}
-(void) connection:(NSURLConnection *)connection didFailWithError: (NSError *)error {
NSLog(@"%@",[error localizedDescription]);
}
- (void) connectionDidFinishLoading: (NSURLConnection*) connection {
NSLog(@"请求完成...");
NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:self.datas options:NSJSONReadingAllowFragments error:nil];
}
GET方法是向指定资源发出请求,只用在读取数据。POST方法是向指定资源提交数据,请求服务器进行处理
同步请求是指:发送方发出数据后,等接收方发回响应以后才发下一个数据包的通讯方式。
异步请求是指:发送方发出数据后,不等接收方发回响应,接着发送下个数据包的通讯方式。
MKNetworkKit框架 GET请求
/*
* 开始请求Web Service
*/
-(void)startRequest
{
NSDictionary *parameter=@{@"format": @"2",@"cityname": @"南京",@"key": @"1174d1a31d33b1dacb69d15c7756f898"};
MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@"v.juhe.cn" customHeaderFields:nil]; //HostName不能加HTTP://
MKNetworkOperation *op = [engine operationWithPath:@"/weather/index" params:parameter httpMethod:@"GET" ssl:NO];
[op addCompletionHandler:^(MKNetworkOperation *operation) {
NSLog(@"responseData : %@", [operation responseString]); //显示中文,但输出无格式
NSData *data = [operation responseData];
NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"数据:%@",resDict); //不显示中文,但输出有格式
} errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
NSLog(@"MKNetwork请求错误 : %@", [err localizedDescription]);
}];
[engine enqueueOperation:op];
}
MKNetworkKit框架 POST请求 待修改
/*
* 开始请求Web Service
*/
-(void)startRequest
{
NSString *path = [[NSString alloc] initWithFormat:@"/weather/index"];
NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
[param setValue:@"2" forKey:@"format"];
[param setValue:@"苏州" forKey:@"cityname"];
[param setValue:@"1174d1a31d33b1dacb69d15c7756f898" forKey:@"key"];
MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@"v.juhe.cn" customHeaderFields:nil];
MKNetworkOperation *op = [engine operationWithPath:path params:param httpMethod:@"POST"];
[op addCompletionHandler:^(MKNetworkOperation *operation) {
NSLog(@"responseData : %@", [operation responseString]);
NSData *data = [operation responseData];
NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
[self reloadView:resDict];
} errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
NSLog(@"MKNetwork请求错误 : %@", [err localizedDescription]);
}];
[engine enqueueOperation:op];
}
标签:
原文地址:http://www.cnblogs.com/saurik/p/4820007.html