AFNetWorking怎么上传XML格式的数据进行POST请求
1个回答
展开全部
随着asihttprequest的停止更新,许多人都转向了AFNetworking、 MKNetworkKit.我也是其中一个。于是我从网上找了许多文章作参考,但是结果都是失败告终。研究了好久都搞不透,最后还是请人帮忙搞定了。经常从网上索取免费资料的一员,要有回报的思想,也为了让更多的人少走些弯路,所以下面是代码:(有错误可以指出)
首先:将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);
}];
可以参考一下。AFNetworking-2.0.3
首先:将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);
}];
可以参考一下。AFNetworking-2.0.3
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询