【问题标题】:Afnetworking 2.0 post method errorAfnetworking 2.0 发布方法错误
【发布时间】: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


    【解决方案1】:

    使用AFHTTPSessionManager 和它的方法POST:parameters:success:failure:。您可以在此处找到更多示例:http://www.raywenderlich.com/59255/afnetworking-2-0-tutorialHooking into the Live Service 标题下。

    【讨论】:

    • 我在使用 afhttpsessionmanager 执行时遇到了同样的错误
    • 在 sessionManager 中,paramiteres 需要提供 NSDictionary 而不是 NSData。
    • 我在代码中提供了字典作为参数。但是没有用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 2014-02-24
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多