本文介绍的是AFNetworking-2.0 使用方法(增加适应:不完善的head內的 meta的content格式)
随着asihttprequest的停止更新,许多人都转向了AFNetworking、 MKNetworkKit.我也是其中一个。于是我从网上找了许多文章作参考,但是结果都是失败告终。研究了好久都搞不透,最后还是请人帮忙搞定了。经常从网上索取免费资料的一员,要有回报的思想,也为了让更多的人少走些弯路,所以下面是代码:(有错误可以指出)
首先:将AFNetworking、UIKit+AFNetworking 加入到工程
一般要引用这两个包,不然会有错误提示:SystemConfiguration.framework、 MobileCoreServices.framework
然后在要使用的地方
#import "AFHTTPRequestOperationManager.h"
#import "AFHTTPSessionManager.h"
AFHTTPRequestOperationManager的post有两个方法,一个是普通的post,另一个是可以上传图片的
1.上传图片:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"text/html"];
NSDictionary *parameters =@{@"参数1":@"value1",@"参数2":@"value2"、、、};
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"1.png"], 1.0);
[manager POST:@"替换成你要访问的地址"parameters:parametersconstructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData :imageData name:@"1" fileName:@"1.png" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation,id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation,NSError *error) {
NSLog(@"Error: %@", error);
}];
这个方法可以上传图片,如果不用上传图片,可以把这句去掉[formData
appendPartWithFileData :imageDataname:@"1"fileName:@"1.png"mimeType:@"image/jpeg"]
2.普通的post
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"text/html"];
NSDictionary *parameters =@{@"参数1":@"value1",@"参数2":@"value2"、、、};
[managerPOST:@"替换成你要访问的地址"parameters:parameters
success:^(AFHTTPRequestOperation *operation,id responseObject) {
NSLog(@"Success: %@", responseObject);
}failure:^(AFHTTPRequestOperation *operation,NSError *error) {
NSLog(@"Error: %@", error);
}];
3.增加适应:不完善的head內的 meta的content格式(2014年3月17日11:20)
引用:http://www.cocoachina.com/bbs/simple/?t176000.html
tom19830924 | 2013-12-27 17:44 |
這是我第二次回答這問題了 這不是AFNetworking的問題 這是做Server那邊的人沒把head內的 meta的content格式指定好 請參考 |
evangel | 2014-03-18 21:41 |
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; |
解决方法:我使用的是evangel的方法,增加下面4句:
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
下面是完整的请求设置:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"text/html"];
manager.requestSerializer = [AFJSONRequestSerializerserializer];
manager.responseSerializer = [AFJSONResponseSerializerserializer];
[manager.requestSerializersetValue:@"application/json"forHTTPHeaderField:@"Accept"];
[manager.requestSerializersetValue:@"application/json; charset=utf-8"forHTTPHeaderField:@"Content-Type"];