AFNetworking 使用方法(2.0)
首先:将AFNetworking、UIKit+AFNetworking 加入到工程
一般要引用这两个包,不然会有错误提示: 然后在要使用的地方
#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
http://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type
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"];