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

时间:2013-06-05 16:47:21

标签: ios objective-c cocoa afnetworking

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

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

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

我怎么能看到普通(NON-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];

1 个答案:

答案 0 :(得分:9)

successerror区块中,您可以NSLog operation.responseStringoperation.responseData。这将为您提供从服务器发送的原始字符串或字节。