AFNetworking 2.4.0 Post请求使用:上传图片+文字
http://cocoadocs.org/docsets/AFNetworking/2.4.0/
#import
"AFNetworking.h"
-(
void
)postRequest{
NSString *str = @"url地址" ;
NSDictionary *userInfo = @{
@"pic" : @"" ,
@"content" : @"asdhyfmhmfsdfsdf" ,
} ;
}
NSString *str = @"url地址" ;
NSDictionary *userInfo = @{
@"pic" : @"" ,
@"content" : @"asdhyfmhmfsdfsdf" ,
} ;
NSURL
*imagePath = [
NSURL
fileURLWithPath
:[[
NSBundle
mainBundle
]
pathForResource
:
@"ko"
ofType
:
@"png"
]];
NSURL
*imagePath2 = [
NSURL
fileURLWithPath
:[[
NSBundle
mainBundle
]
pathForResource
:
@"1"
ofType
:
@"png"
]];
// NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ko" ofType:@"png"]];
// NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"ko.png" withExtension:nil];
AFHTTPRequestOperationManager *manager = [ AFHTTPRequestOperationManager manager ];
[manager POST :str parameters :userInfo constructingBodyWithBlock :^( id < AFMultipartFormData > formData) {
[formData appendPartWithFileURL :imagePath name : @"pic" error : nil ];
// NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ko" ofType:@"png"]];
// NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"ko.png" withExtension:nil];
AFHTTPRequestOperationManager *manager = [ AFHTTPRequestOperationManager manager ];
[manager POST :str parameters :userInfo constructingBodyWithBlock :^( id < AFMultipartFormData > formData) {
[formData appendPartWithFileURL :imagePath name : @"pic" error : nil ];
[formData
appendPartWithFileURL
:imagePath2
name
:
@"pic"
error
:
nil
];
// [formData appendPartWithFormData:data name:@"pic"];
} success :^( AFHTTPRequestOperation *operation, id responseObject) {
NSLog ( @"Success: %@" , responseObject);
} failure :^( AFHTTPRequestOperation *operation, NSError *error) {
NSLog ( @"Error: %@" , error);
}];
// [formData appendPartWithFormData:data name:@"pic"];
} success :^( AFHTTPRequestOperation *operation, id responseObject) {
NSLog ( @"Success: %@" , responseObject);
} failure :^( AFHTTPRequestOperation *operation, NSError *error) {
NSLog ( @"Error: %@" , error);
}];
}
之前一直报错,打印错误信息:Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: not found (404)" UserInfo=0x10c2573a0 {NSUnderlyingError=0x10b9524c0 "Request failed: unacceptable content-type: text/html", com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x10b93b0b0> {
404的错误,我以为是我这边的url不对才导致的404错误。后来才知道是服务器端口改了。以后做网络请求的时候,一定要先把url用浏览器打开看看能不能成功