iOS进行网络请求时,使用NSURLConnection方法。
(1)NSURLConnection主要结合NSURL,NSURLRequest使用
(1-1)NSURL用来设置网络请求的地址
(1-2)NSURLRequest用来设置url请求,以及设置请求时相关参数设置(如:缓存、超时等)
(2)NSURLConnection既有代理方式,又有block代码块的同步,或异步方式
1、代理方式的使用
1-1、设置协议
NSURLConnectionDelegate
NSURLConnectionDataDelegate
1-2、实始化等操作
// 图片地址
// NSString *string = @"http://img4.imgtn.bdimg.com/it/u=819265564,3078214620&fm=206&gp=0.jpg";
// 天气地址
NSString *string = @"http://api.k780.com:88/?app=weather.future&weaid=1&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
string = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // 请求地址中包含中文时进行转换
// 创建URL
NSURL *url = [NSURL URLWithString:string];
// 创建request
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url];
// 或
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
// 缓存
mutableRequest.cachePolicy = NSURLRequestUseProtocolCachePolicy;
// 请求超时
mutableRequest.timeoutInterval = 30.0;
// 请求方式POST、或GET
// mutableRequest.HTTPMethod = @"POST";
mutableRequest.HTTPMethod = @"GET";
// 创建connection
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:mutableRequest delegate:self];
// 开始请求
[connection start];
// 停止请求
[connection cancel];
1-3、代理方法
// 实现代理方法
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"1 请求失败");
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"2 请求成功收到请求头信息 response = %@", response);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"3 请求成功收到请求信息 data = %@", data);
if (0 != data.length)
{
if (connection == self.connection)
{
// 天气信息数据
[self.networkData appendData:data];
}
else if (connection == self.imageConnection)
{
// 图片数据
if (self.imageData == nil)
{
self.imageData = [[NSMutableData alloc] init];
}
[self.imageData appendData:data];
}
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"4 请求成功结束请求");
if (connection == self.connection)
{
self.networkDict = [NSJSONSerialization JSONObjectWithData:self.networkData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"resultDict = %@", self.networkDict);
if (self.networkDict && 0 != self.networkDict.count)
{
[self.tableview reloadData];
}
}
else if (connection == self.imageConnection)
{
UIImage *image = [UIImage imageWithData:self.imageData];
self.imageview.image = image;
}
}
2、block方式的使用
// 图片地址
NSString *string = @"http://img4.imgtn.bdimg.com/it/u=819265564,3078214620&fm=206&gp=0.jpg";
// 天气地址
NSString *string = @"http://api.k780.com:88/?app=weather.future&weaid=1&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
string = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // 请求地址中包含中文时进行转换
NSURL *url = [NSURL URLWithString:string];
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url];
// 或
// NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
// 缓存
mutableRequest.cachePolicy = NSURLRequestUseProtocolCachePolicy;
// 请求超时
mutableRequest.timeoutInterval = 30.0;
// 请求方式POST、或GET
// mutableRequest.HTTPMethod = @"POST";
mutableRequest.HTTPMethod = @"GET";
// block-异步,无停止方法
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:self.request queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (0 != data.length)
{
UIImage *image = [UIImage imageWithData:data];
self.imageview.image = image;
}
}];
// block-同步,无停止方法
NSData *data = [NSURLConnection sendSynchronousRequest:self.request returningResponse:nil error:nil];
if (0 != data.length)
{
UIImage *image = [UIImage imageWithData:data];
self.imageview.image = image;
}