Afnetworking 2.0 post方法错误

时间:2014-11-13 09:01:36

标签: ios http-post afnetworking-2

我想使用afnetworking 2.0将数据发送到网址。我将字典值作为参数传递。以下是我的代码。

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,headers {     连接=关闭;     “Content-Language”= en;     “内容长度”= 968;     “Content-Type”=“text / html; charset = utf-8”;     Date =“星期四,2014年11月13日08:52:14 GMT”;     Server =“Apache-Coyote / 1.1”; },NSErrorFailingURLKey =我的网址,NSLocalizedDescription =请求失败:错误请求(400),com.alamofire.serialization.response.error.data =< 3c68746d 6c3e3c68 6561643e 3c746974 6c653e41 70616368 6520546f 6d636174 2f372e30 2e353420 2d204572 726f7220 7265706f 72743c2f 7469746c 653e3c73 74796c65 3e3c212d 2d483120 7b666f6e 742d6661 6d696c79 3a546168 6f6d612c 41726961 6c2c7361 6e732d73 65726966 3b636f6c 6f723a77 68697465 3b626163 6b67726f 756e642d 636f6c6f 723a2335 32354437 363b666f 6e742d73 697a653a 32327078 3b7d2048 32207b66 6f6e742d 66616d69 6c793a54 61686f6d 612c4172 69616c2c 73616e73 2d736572 69663b63 6f6c6f72 3a776869 74653b62 61636b67 726f756e 642d636f 6c6f723a 23353235 4437363b 666f6e74 2d73697a 653a3136 70783b7d 20483320 7b666f6e 742d6661 6d696c79 3a546168 6f6d612c 41726961 6c2c7361 6e732d73 65726966 3b636f6c 6f723a77 68697465 3b626163 6b67726f 756e642d 636f6c6f 723a2335 32354437 363b666f 6e742d73 697a653a 31347078 3b7d2042 4f445920 7b666f6e 742d6661 6d696c79 3a546168 6f6d612c 4 1726961 6c2c7361 6e732d73 65726966 3b636f6c 6f723a62 6c61636b 3b626163 6b67726f 756e642d 636f6c6f 723a7768 6974653b 7d204220 7b666f6e 742d6661 6d696c79 3a546168 6f6d612c 41726961 6c2c7361 6e732d73 65726966 3b636f6c 6f723a77 68697465 3b626163 6b67726f 756e642d 636f6c6f 723a2335 32354437 363b7d20 50207b66 6f6e742d 66616d69 6c793a54 61686f6d 612c4172 69616c2c 73616e73 2d736572 69663b62 61636b67 726f756e 643a7768 6974653b 636f6c6f 723a626c 61636b3b 666f6e74 2d73697a 653a3132 70783b7d 41207b63 6f6c6f72 203a2062 6c61636b 3b7d412e 6e616d65 207b636f 6c6f7220 3a20626c 61636b3b 7d485220 7b636f6c 6f72203a 20233532 35443736 3b7d2d2d 3e3c2f73 74796c65 3e203c2f 68656164 3e3c626f 64793e3c 68313e48 54545020 53746174 75732034 3030202d 203c2f68 313e3c48 52207369 7a653d22 3122206e 6f736861 64653d22 6e6f7368 61646522 3e3c703e 3c623e74 7970653c 2f623e20 53746174 75732072 65706f72 743c2f70 3e3c703e 3c623e6d 65737361 67653c2f 623e203c 753e3c2f 753e3c2f 703e3c70 3e3c623e 64657363 72697074 696f6e3c 2f623e20 3c 753e54 68652072 65717565 73742073 656e7420 62792074 68652063 6c69656e 74207761 73207379 6e746163 74696361 6c6c7920 696e636f 72726563 742e3c2f 753e3c2f 703e3c48 52207369 7a653d22 3122206e 6f736861 64653d22 6e6f7368 61646522 3e3c6833 3e417061 63686520 546f6d63 61742f37 2e302e35 343c2f68 333e3c2f 626f6479 3e3c2f68 746d6c3e>中NSUnderlyingError = 0x8c1e0e0“请求失败:不可接受的内容类型:的text / html“}

我是IOS的新手,对afnetworking不太了解。有人会为此重播吗?

1 个答案:

答案 0 :(得分:0)

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