문제

I am in the process of switching over some of my code from AFNetworking 1.0 to 2.0.

Before when doing a POST, I was creating an AFHTTPClient, and an AFHTTPRequestOperation like so:

 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.....
  });

This worked fine, and my POSTs went through and I received a successful text response from the server. (which I then converted to a NSDictionary)

I now am using an AFHTTPSessionManager singleton, and calling the POST method from that. When initializing my AFHTTPSessionManager, I am doing the following:

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

Then in my other class, I am calling the POST like so:

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
});

My data has not changed at all, but the POSTs always fail with the error:

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";

Whats different that is causing the new AFNetworking 2.0 POST code to not work with this now? Is there anything I need to be setting? The URL and Parameters I am passing are the same as they were with the old way I was sending the POST.

Thanks

도움이 되었습니까?

해결책

My solution ended up being a pretty simple one

In my AFHTTPSessionManager's init, I was not setting the RequestSerializer along with the ResponseSerializer.

After setting it correctly, my POSTs are going through fine again. Heres what I set:

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

[self setRequestSerializer:[AFJSONRequestSerializer serializer]];

EDIT

Aaron Brager stated that those first 2 lines are defaults and not needed. All I needed was to set the RequestSerializer. I tested and can verify this.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top