【发布时间】:2014-11-13 09:01:36
【问题描述】:
我想使用 afnetworking 2.0 将数据发送到 url。我将字典值作为参数传递。以下是我的代码。
NSError* 错误; NSData *executionsData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"my url"]];
[request setTimeoutInterval:600];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)executionsData.length] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:executionsData];
[request setValue:[self userAgent] forHTTPHeaderField:@"User-Agent"];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary* resultsDict = (NSDictionary*)responseObject;
NSLog(@"%@", resultsDict);
//process result json dictionary
//success
//success();
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//failure
NSLog(@"Error:%@",error);
//failure();
}];
但每次执行此代码时,我都会收到如下错误。
Domain=com.alamofire.error.serialization.response Code=-1011 "请求失败:错误请求 (400)" UserInfo=0x8c46db0 {com.alamofire.serialization.response.error.response= { URL: my url } {状态码:400,标题{ 连接=关闭; "内容-语言" = en; “内容长度”= 968; “内容类型”=“文本/html;字符集=utf-8”; 日期 =“2014 年 11 月 13 日星期四 08:52:14 GMT”; 服务器 = "Apache-Coyote/1.1"; }},NSErrorFailingURLKey =我的网址,NSLocalizedDescription =请求失败:坏请求(400),com.alamofire.serialization.response.error.data = ,NSUnderlyingError = 0x8c1e0e0“请求失败:不可接受的内容类型:文本/html"}
我是IOS新手,对afnetworking不太了解。有人会重播这个吗?
【问题讨论】:
标签: ios http-post afnetworking-2