处理网络请求的类 :
1.NSURLConnection:苹果官方提供,使用较为麻烦
2.ASIHTTPRequest:功能强大,但是较早就停止更新
3.AFNetworking:目前使用的最多的
************ AFNetworking功能特点 ************
AFNetworking:网络数据请求的第三方库
1. get/post请求
2. 下载/上传文件
3. 处理Json/plist文件响应
AFHTTPRequestOperationManager 创建的管理对象,黙认可以处理Json/Plist响应
reponse.MIMEType ---> application/json text/json
************************AFNetworking*********************
// 1.用类方法创建管理对象
AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManagermanager];
// 2.根据服务器内容不同指定解析器
//manager 黙认情况下是解析 json plist
// 解析xml 需手动设置
//manager.responseSerializer 这个方法进行解析器的设置
//manager.requestSerializer 这个方法进行解析器的设置
//aplication/json text/json
// 3.通过管理对象manager发送get/post请求
//GET请求
//参数1:接口地址 urlString
//参数2:请求参数
//参数3:网络请求成功后的回调 blocks
[manager GET:<#(NSString *)#> parameters:<#(id)#> success:^(AFHTTPRequestOperation *operation, id responseObject) {
//请求成功后的操作
//operation.resposeData
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//请求失败后的操作
NSLog(@"%@",error);
UIAlertView
}];
//post请求
//参数1:接口地址 urlString
//参数2:请求参数
//参数3:网络请求成功后的回调 blocks
[manager POST:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//发送成功后的回调
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//发送失败后的回调
}];
************************SDWebImage*********************
#import "UIImageView+WebCache.h"//实现网络加载图片必须用这个
#define imageURL @"http://cdn.duitang.com/uploads/item/201407/23/20140723130120_XkRdX.jpeg"
//获取一张网络图片,并加载到本地
UIImageView *iv = [[UIImageViewalloc] initWithFrame:[UIScreenmainScreen].bounds];
[self.view addSubview:iv];
// 第一种方法:网络下载数据然后把数据转为图片 再加载
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"image/jpeg"];
[manager GET:imageURL parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {
NSLog(@"下载图片成功!");
iv.image = [UIImage imageWithData:operation.responseData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];
// 第二种方法:SDWebImage
[iv sd_setImageWithURL:[NSURLURLWithString:imageURL]];