AFN (AFNetworking)
網(wǎng)絡請求中, 使用最多的就是AFNetworking框架, AFNetworking是一個輕量級的iOS網(wǎng)絡通信類庫峡迷。它建立在NSURLConnection和ios 7.0 后推出的NSURLSession等類庫的基礎上,讓很多網(wǎng)絡通信功能的實現(xiàn)變得十分簡單。它支持HTTP請求和基于REST的網(wǎng)絡服務(包括GET、POST、 PUT等)伍掀。支持ARC。 使用AFN的經(jīng)常會遇到以下錯誤, 導致請求或者響應失敗, 主要原因就在于AFN請求和響應的默認的數(shù)據(jù)解析格式, 如果與默認的不一致就會發(fā)生解析錯誤, 導致請求和響應失敗
1.response響應
response響應,默認會把數(shù)據(jù)轉成JSON格式, 如果服務器返回的是JSON格式的還好, 如果 返回的是html, 字符串等等格式, 就不能用response的默認格式, 讓返回數(shù)據(jù)是什么格式就呈現(xiàn)什么格式, 但是二進制數(shù)據(jù)沒法看懂, 需要轉為了字符串, alloc,initWithData..
- 響應錯誤的解決辦法如下:
.
simple demo:
.
//請求百度, 常見錯誤
- (void)demo{
//用自定義的繼承自AFHTTPSessionManager的類 創(chuàng)建 NSURLSession的管理者
NetWorkTools * netWorkTool = [NetWorkTools sharedNetWorkTools];
/**
響應回來,默認的序列化方法是 JSON
不使用默認的數(shù)列化方式, 采用普通的序列化方式, 返回二進制
html
*/
netWorkTool.responseSerializer = [AFHTTPResponseSerializer serializer];
//發(fā)送請求
[netWorkTool GET:@"http://www.baidu.com" parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
NSLog(@"%@--%@",responseObject,[responseObject class]);
//將服務器返回回來的二進制數(shù)據(jù)轉化為html,也就是字符串
NSString * html = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"html = %@",html);
} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
NSLog(@"%@",error.localizedDescription);
}];
}
// NetWorkTools.m
#import "NetWorkTools.h"
static NSString * const BASEURL = @"";
@implementation NetWorkTools
//定義靜態(tài)的,全局都可以使用
static NetWorkTools * _netWorkTools;
+ (instancetype)sharedNetWorkTools{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_netWorkTools = [[self alloc] initWithBaseURL:[NSURL URLWithString: BASEURL]];
_netWorkTools.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
});
return _netWorkTools;
}
@end
.
2.request請求
request請求, 默認的會把數(shù)據(jù)轉為NSData, 但是服務器只能接受JSON格式的數(shù)據(jù), 才能進行反序列化, 因為后臺php只接受JSON格式數(shù)據(jù), 所以不能使用request默認的格式, 需要轉化為服務器接受的JSON格式
請求錯誤的解決辦法如下:
.
simple demo
.
//POST JSON
- (void)demo1{
//POST請求,發(fā)送數(shù)據(jù)給服務器,交互時, 由于后臺php語言只接受JSON格式的數(shù)據(jù),那什么樣的數(shù)據(jù)可以轉為JSON格式呢?
//1.頂部節(jié)點是 字典 或者 數(shù)組
//2.普通的字符串
//3.自定義的類, 需要模型轉字典,KVC, 然后才能轉為JSON格式
//自定義的類 創(chuàng)建 一個session的管理者
NetWorkTools * netWorkTool = [NetWorkTools sharedNetWorkTools];
//發(fā)送的參數(shù)是普通的字符串, 這種攜帶用戶信息的參數(shù), 最好使用POST方式request,不會緩存, 也沒有長度限制, 并且不會放在URL后面相對安全
NSDictionary * params = @{
@"username":@"abc",
@"password":@"123"
};
//由于服務器后臺語言是php, 只接受JSON格式, 才會進行序列化解析
netWorkTool.requestSerializer = [AFJSONRequestSerializer serializer];
//服務器返回的是純凈的二進制
netWorkTool.responseSerializer = [AFHTTPResponseSerializer serializer];
//需要提交數(shù)據(jù)給服務器, 需要進行交互的 用POST
[netWorkTool POST:@"http://localhost/post/postjson.php" parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
NSLog(@"%@--%@",responseObject,[responseObject class]);
//將服務器返回的二進制轉為 text/plain
NSString * result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"result = %@",result);
} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
NSLog(@"%@",error.localizedDescription);
}];
}
#import "NetWorkTools.h"
static NSString * const BASEURL = @"";
@implementation NetWorkTools
//定義靜態(tài)的,全局都可以使用
static NetWorkTools * _netWorkTools;
+ (instancetype)sharedNetWorkTools{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_netWorkTools = [[self alloc] initWithBaseURL:[NSURL URLWithString: BASEURL]];
_netWorkTools.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
});
return _netWorkTools;
}
@end