(一). 项目集成 (由于我这个是老项目,所以用的是最原始的集成方法)
1.下载最新源码:下载地址
2.然后直接把下载下来的源码文件夹AFNetworking直接拖入工程。
3.添加类库 Security.framework、MobileCoreServices.framework、SystemConfiguration.framework
4. 然后在预处理文件中添加 #import "AFNetworking.h"
5. 大功告成
(二). 常用用法
1. GET
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
[session GET:@"请求的url" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"成功");
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"失败");
}];
2. POST
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *string = [user objectForKey:USER_MAGAZINE_TIMESTAMPS];
if (string == nil) {
return;
}
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *dict = @{@"time_stamps":string};
[manager POST:MAGAZINE_NO_READ_NUMBER parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary* magazineDic = (NSDictionary *)responseObject;
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error %@",error);
}];
3. download
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);
}];
[downloadTask resume];
4. upload
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"Success: %@ %@", response, responseObject);
}
}];
[uploadTask resume];
(三). 项目遇到的问题
第一个问题: 集成完成AF以后,我就迫不及待的使用它的post方法,出现了以下的error信息。
Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x1468a1f80> { URL: https://magalive.sizzee.com:8089/servlet/GetNoReadMagaCountServlet } { status code: 200, headers {
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Type" = "text/html;charset=utf-8";
Date = "Tue, 24 May 2016 03:34:19 GMT";
"Set-Cookie" = "SERVERID=a01bf0c5559d5d808207dc9e02fa4f60|1464060859|1464060856;Path=/";
"Transfer-Encoding" = Identity;
Vary = "Accept-Encoding";
} }, NSErrorFailingURLKey=https://magalive.sizzee.com:8089/servlet/GetNoReadMagaCountServlet, com.alamofire.serialization.response.error.data=<7b22676c 6f62616c 5f696d67 5f75726c 223a2268 74747073 3a2f2f6d 6167617a 696e652e 6d656469 612e7369 7a7a6565 2e636f6d 222c226e 6f526561 64436f75 6e74223a 307d>, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}
大家可以很清楚的发现,这是返回类型不匹配所导致的。 那怎么解决这个问题呢。
在使用AFHTTPSessionManager的时候,加上下面第二句代码。把text/html添加到集合中。
第一种解决方法:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
在AFNetworking文件夹中AFURLResponseSerialization.m文件中,找到初始化acceptableContentTypes的位置,修改成下面这句。 其实就是在原有的基础上添加了@"text/html"。
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
第二个问题是.给服务器传参数老是传不过去?
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer]; //申明请求的类型是json类型
manager.responseSerializer = [AFJSONResponseSerializer serializer]; //申明返回的类型是json类型
看以上代码,可以发现我指定了请求的类型是json方式传给服务器,但是实际上我却没有以json的格式吧参数传给服务器,所以导致AF直接把我的参数过滤掉,所以服务器就一直收不到参数,解决方法就是,直接注释掉
manager.requestSerializer = [AFJSONRequestSerializer serializer];
这句代码。