IOS 网络请求

 

技术交流新QQ群:414971585html

 

关于网络请求的重要性我想不用多说了吧。对于移动客户端来讲,网络的重要性不言而喻。常见的网络请求有同步GET, 同步POST, 异步GET, 异步POST。今天来看一下四种网络请求的实现方式。ios

1、同步GETjson

1
2
3
4
5
6
7
8
9
10
11
12
// 1.将网址初始化成一个OC字符串对象
NSString *urlStr = [NSString stringWithFormat:@ "%@?query=%@®ion=%@&output=json&ak=6E823f587c95f0148c19993539b99295" , kBusinessInfoURL, @ "银行" , @ "济南" ];
// 若是网址中存在中文,进行URLEncode
NSString *newUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// 2.构建网络URL对象, NSURL
NSURL *url = [NSURL URLWithString:newUrlStr];
// 3.建立网络请求
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
// 建立同步连接
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

当建立好同步连接之后, 就能够采用相应的方法进行解析。下面建立异步链接也是同样的。安全

2、同步POST服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 1.根据网址初始化OC字符串对象
     NSString *urlStr = [NSString stringWithFormat:@ "%@" , kVideoURL];
     // 2.建立NSURL对象
     NSURL *url = [NSURL URLWithString:urlStr];
     // 3.建立请求
     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
     // 4.建立参数字符串对象
     NSString *parmStr = @ "method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10" ;
     // 5.将字符串转为NSData对象
     NSData *pramData = [parmStr dataUsingEncoding:NSUTF8StringEncoding];
     // 6.设置请求体
     [request setHTTPBody:pramData];
     // 7.设置请求方式
     [request setHTTPMethod:@ "POST" ];
     
     // 建立同步连接
     NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

3、异步GET网络

1
2
3
4
5
6
7
8
9
10
11
     NSString *urlStr = [NSString stringWithFormat:@ "http://image.zcool.com.cn/56/13/1308200901454.jpg" ];
     NSString *newStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     NSURL *url = [NSURL URLWithString:newStr];
     NSURLRequest *requst = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
     //异步连接(形式1,较少用)
     [NSURLConnection sendAsynchronousRequest:requst queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
         self.imageView.image = [UIImage imageWithData:data];
         // 解析
         NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
         NSLog(@ "%@" , dic);
     }];

4、异步POSTapp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// POST请求
     NSString *urlString = [NSString stringWithFormat:@ "%@" ,kVideoURL];
     //建立url对象
     NSURL *url = [NSURL URLWithString:urlString];
     //建立请求
     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
     //建立参数字符串对象
     NSString *parmStr = [NSString stringWithFormat:@ "method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10" ];
     //将字符串转换为NSData对象
     NSData *data = [parmStr dataUsingEncoding:NSUTF8StringEncoding];
     [request setHTTPBody:data];
     [request setHTTPMethod:@ "POST" ];
     //建立异步链接(形式二)
     [NSURLConnection connectionWithRequest:request delegate:self];

通常的,当建立异步链接时, 不多用到第一种方式,常常使用的是代理方法。关于NSURLConnectionDataDelegate,咱们常常使用的协议方法为一下几个:异步

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 服务器接收到请求时
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
}
// 当收到服务器返回的数据时触发, 返回的多是资源片断
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
}
// 当服务器返回全部数据时触发, 数据返回完毕
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
}
// 请求数据失败时触发
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
     NSLog(@ "%s" , __FUNCTION__);
}

最后,分析一下这几种呢网络请求的区别。ide

GET请求和POST请求的区别:url

1. GET请求的接口会包含参数部分,参数会做为网址的一部分,服务器地址与参数之间经过 ? 来间隔。POST请求会将服务器地址与参数分开,请求接口中只有服务器地址,而参数会做为请求的一部分,提交后台服务器。

2. GET请求参数会出如今接口中,不安全。而POST请求相对安全。

3.虽然GET请求和POST请求均可以用来请求和提交数据,可是通常的GET多用于从后台请求数据,POST多用于向后台提交数据。

同步和异步的区别:

同步连接:主线程去请求数据,当数据请求完毕以前,其余线程一概不响应,会形成程序就假死现象。

异步连接:会单独开一个线程去处理网络请求,主线程依然处于可交互状态,程序运行流畅。

 

原文源自:http://www.cocoachina.com/ios/20140919/9691.html

相关文章
相关标签/搜索