AFNetworking 2.0 POST问题

时间:2014-05-15 16:18:04

标签: objective-c cocoa ios7 afnetworking afnetworking-2

我正在将部分代码从AFNetworking 1.0切换到2.0。

在进行POST之前,我创建的是AFHTTPClient,而AFHTTPRequestOperation就像这样:

 AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:reqUrl];
            [httpClient setParameterEncoding:AFJSONParameterEncoding];
            httpClient.operationQueue.maxConcurrentOperationCount = 1;
            NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                                    req.viewName, @"viewName",
                                    req.json, @"JSON",
                                    req.dateAdded.description, @"dateTime",
                                    req.latitude, @"latitude",
                                    req.longitude, @"longitude",
                                    req.heading, @"heading",
                                    req.user, @"requestUser",
                                    nil];


 AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
                [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

 [op setCompletionBlockWithSuccess:
                 ^(AFHTTPRequestOperation *operation,
                   id responseObject) {
  .......convert responseObject (string) to NSDictionary.....
  });

这很好用,我的POST经历了,我收到了来自服务器的成功文本响应。 (然后我转换为NSDictionary

我现在正在使用AFHTTPSessionManager单例,并从中调用POST方法。在初始化我的AFHTTPSessionManager时,我正在执行以下操作:

AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
[self setResponseSerializer:responseSerializer];
self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];

然后在我的其他课程中,我这样调用POST:

NSDictionary *params = @{
                       @"viewName":req.viewName,
                       @"JSON":req.json,
                       @"dateTime":req.dateAdded.description,
                       @"latitude":req.latitude,
                       @"longitude":req.longitude,
                       @"heading":req.heading,
                       @"requestUser":req.user
                       };

[netManager POST:path parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
.....
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    //failing here
});

我的数据根本没有改变,但POST总是因错误而失败:

Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: bad request (400)" UserInfo=0x1704675c0 {AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x178234660> { URL: ... } { status code: 400, headers {
    "Content-Length" = 2738;
    "Content-Type" = "text/html";
    Date = "Thu, 15 May 2014 16:13:51 GMT";
    Server = "Microsoft-IIS/7.0";
    "X-Powered-By" = "ASP.NET";

有什么不同导致新的AFNetworking 2.0 POST代码现在无法使用?有什么我需要设置的吗?我传递的URL和参数与我发送POST的旧方式相同。

由于

1 个答案:

答案 0 :(得分:1)

我的解决方案最终变得非常简单

在我的AFHTTPSessionManager's初始版中,我没有将RequestSerializerResponseSerializer一起设置。

正确设置后,我的POST再次正常运行。这是我设定的内容:

[self setResponseSerializer:[AFJSONResponseSerializer serializer]]; self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@“application / json”,nil];

[self setRequestSerializer:[AFJSONRequestSerializer serializer]];

编辑

Aaron Brager表示,前两行是默认值,不需要。我只需要设置RequestSerializer。我测试过并且可以验证这一点。