之前一直使用ASIHttpRequest 做網絡請求 ,后來新公司用AFNetWorking ,經過一段時間學習總結一下二者的優缺點:
1.AFNetWorking的優缺點
優點:
1.維護和使用者比較多
2.對ARC支持
3.對Block的支持非常好
4.專門對異步請求的支持
5.支持基於URL 緩存
6.圖片以及NSURLCache AFCache緩存使用
7.支持后台下載,緩存離線請求的支持(SDURLCache)
8.對JSON支持比較好
9.對實現的http請求比較容易
10.對HTTPS 的支持
缺點:
是基於Foundation框架的NSURLConnection對網絡進行的封裝,不是用的底層的CFNetwork進行的封裝,擴展功能可能稍微少一些,
但是在http上的網絡支持已經足夠。
二.ASIHttpRequest
優點:
1.對網絡底層CFNetwork支持
2.對代理支持設置比較方便
3.支持離線下載和基於URL的緩存
4.對HTTPS的支持
5.對http 請求方面的實現不是太好,會多寫點代碼
6.同步和異步的支持
用法:
1.首先將AFNetWorking(主要基於網絡請求的方法) UIKit+AFNetworking(異步加載圖片的方法)加入到工程 一般引用這兩個包,不然會有提示錯誤 SystemConfiguration.framework、
MobileCoreServices.framework
2.在使用的地方 #import"AFHTTPRequestOperationManager"
#import"AFHTTPSessionManager"
AFHTTPOperationManager 的post有兩個方法, 一個是普通的post ,另一個是可以上傳圖片
1.上傳圖片
AFHTPOperationMAnager*manager=[AFHTTPOperationManager manager];
manager.responseSerializer.acceptableContentTypes=[NSSetsetWithObject:@"text/html"];
NSDictionary*parameters=@{@"參數1":@"value1",@"參數2":@"value2"....};
NSData*imageData=UIImageJPEGRepresentation([UIImage imageNamed:@"1.png"],1.0);
[manager POST:@"替換成你要訪問的地址"parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>formData){
[formData appendPartWithFileData:imageData name:@"1" fileName:@"1.png" mimeType:@"image/jpep"];
} success:^(AFHTTPRequestOperation*operation,id responseObject){
NSLog(@"success:%@",responseObject);}
failure:^(AFHTTPRequestOperation*operation,ESError*error){
NSLog(@"Error:%@",error);}]
這個方法可以上傳圖片,如果不用上傳圖片,可以吧這句話去掉
[formData appendPartWithFileData:imageData name:@"1" fileName:@"1.png" mimeType:@"image/jpep"];
上傳多張圖片
-(void)uploadPictures{
NSString*domainStr=@"http://192.168.1.69/xffcol/index.php/api/";
AFHTTPRequestOperationManager*manager=[AFHTTPRequestOperationManager manager];
manager.responseSerializar=[AFHTTPResponseSerializar serializer];
//如果還要上傳其他的參數,參考上面的POST請求 ,創建一個可變字典,存入需要提交的參數內容, 作為paramater的參數
[manager POST:domainStr parameters:nil consructingBodyWithBlock:^(id<AFMultipartFormData>formData)
{
//_imageArray 就是圖片數組,我的_imageArray里面存的都是圖片的data,下面可以直接取出來使用,如果存的是image,
//將image轉換成data的方法如下:NSData*imageData=UIImageJPEGRepresentation([UIImage imageNamed:@"1.png"],1.0);
if (_imageArray.count>0){
for(int i=0;i<_imageArray.count;i++){
NSData*data=_imageArray[i]
//上傳的參數名
NSString*name=[NSString stringWithFormat:@"%d",i]
//上傳的filename
NSString*fileName=[NSString stringWithFormat:@"%@.png",name];
[formData appendPartWithFileData:data
name:name
filename:fileName
miimeType:@"image/png"];
}
}
success:^(AFHTTPRequestOperation)operation,id responseObject){
//關閉風火輪
[UIApplication sharedApplication].netorkActivityIndicatorVisiable=NO;
//JSON解析
NSDictionary*resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
NSLog(----------------);
}failure:^(AFHTTPRequestOperation*operation,NSError*error{
//解析失敗隱藏通風火輪(可以打印error.userInfo查看錯誤信息)
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
}];
}
2.普通的post
AFHTPRequestOperationManger*manager=[AFHTTPOperation manager];
manager.responseSerialize.acceptableContentTypes=[NSSetsetWithObject:@"text/html"];
NSDictionary*parameters=@{@"參數1":@"value1",@"參數2":@"value2"....};
[manager POST:@"替換成需要訪問的地址"parameters:
success:^(AFHTTPRequestOperation*operation,id responseObject){
NSLog(@"success:%@",responseObject);}
failure:^(AFHTTPRequestOperation*operation,ESError*error){
NSLog(@"Error:%@",error);}]
二.GET請求網絡數據方式
-(void)obtainData
{
//啟動系統風火輪
[UIApplication shareApplication].networkActicityIndicatorVisible=YES;
//前面寫服務器給的域名,后面拼接上需要提交的參數,假如參數是key=1
NSString*domainStr=@"http://192.168.1.69/xffcol/index.php/Api/key/1/";
AFHTTPOperationManager*manager=[AFHTTPOperationManager manager];
manager.responseSerializer=[AFHTTPResponseSerializer serializer];
//以GET的形式提交,只需要將上面的請求地址給GET做參數就可以
[manager GET:domainStr parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject)
{
//隱藏系統風火輪
[UIApplication sharedApplication].networkActicityIndicatorVisible=NO;
//json解析
NSDictionary *resultDic=[NSJsonSerialization JSONOBjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"---獲取到的json格式的字典---%@",resultDic);
}failure:^(AFHTTPRequestOperation*operation,NSError*error{
//解析失敗隱藏通風火輪(可以打印error.userInfo查看錯誤信息)
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
})]
}
