ios - 如何使 AFNetworking 显示纯 HTTP 响应错误

标签 ios objective-c cocoa afnetworking

我正在尝试将一些信息从我的应用程序发送到我的 PHP 服务器端程序。

当我尝试使用 PHP Array 作为 UnitTest 发送相同的数据时,它可以工作。所以我不知道为什么 AFNeworking 认为这不起作用。

为了调试问题,我需要能够查看完整的 HTTP 消息。

我如何才能看到普通(非 JSON)错误(HTTP 响应)?

我虽然 operation.JSONReadingOptions = NSJSONReadingAllowFragments; 可以解决问题,但不起作用或者我没有将其放在正确的位置

谢谢

我的错误:

2013-06-05 10:52:37.990 iGym[9407:c07]Request failed with error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=0x96db4d0 {NSDebugDescription=Garbage at end.}, {
NSDebugDescription = "Garbage at end.";

}

我的 Objective-c 代码

User* myUser = [self getCurrentUser];
    NSURL *url = [[NSURL alloc]initWithString:@"http://192.168.1.64/"];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];

    NSDictionary *params = @{@"register":@"true",
                             @"email":@"as234d",
                             @"userID":(myUser.idUserExternal ? myUser.idUserExternal: @"NO"),
                             @"userDetails":@{@"needUsername":(myUser.nickname ? @"NO": @"YES"),
                                              @"username":( myUser.nickname  ? myUser.nickname : _input.text),
                                              @"language":(myUser.language? myUser.language: @"0"),
                                              @"name":(myUser.name ? myUser.name : @"0"),
                                              @"surname":(myUser.surname ? myUser.surname : @"0"),
                                              @"country":(myUser.country ? myUser.country : @"0"),
                                              @"dob":(myUser.dob ? myUser.dob : @"0"),
                                              @"city":(myUser.city ? myUser.city : @"0"),
                                              @"height":(myUser.height ? myUser.height : @"0"),
                                              @"weight":(myUser.weight ? myUser.weight : @"0"),
                                              @"metricType":(myUser.metricSystem ? myUser.metricSystem : @"0")
                                            }
                             };
    NSLog(@"%@",params);
    NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"igym/bootstrap.php" parameters:params];


    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                                        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
                                                                                            NSLog(@"Inside the success block %@",JSON);
                                                                                            if ([JSON objectForKey:@"ok"]) {
                                                                                                [self done:JSON];
                                                                                            }

                                                                                            if ([JSON objectForKey:@"error"]) {
                                                                                                [self tryAgain:JSON[@"error"]];
                                                                                            }
                                                                                        }
                                                                                        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){

                                                               NSLog(@"json text is: %@", JSON);
                                                                                            NSLog(@"Request failed with error: %@, %@", error, error.userInfo);
                                                                                        }];
    operation.JSONReadingOptions = NSJSONReadingAllowFragments;

    [operation start];

最佳答案

在您的 successerror block 中,您可以使用 NSLog operation.responseStringoperation。响应数据。这将为您提供从服务器发送的原始字符串或字节。

关于ios - 如何使 AFNetworking 显示纯 HTTP 响应错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16945531/

相关文章:

ios - 将 MPMediaItem 添加到 MPMediaItemCollectino

objective-c - HTTP 直播 Mac 应用程序

objective-c - 将用户数据附加到 AXObserver

ios - masonry 自动布局不起作用

ios - 使用 NSTimer 更改 UIImage?

ios - 如何从 UITableView header 中分离出来?

ios - 我将如何停止在我的表格 View 中重复加载图像?

objective-c - CAGradientLayer 类型——有什么意义?

json - 为什么 cocoa 数组有时会添加引号而不是其他的

cocoa - 如何强制 NSMenuItem 重绘?