ios - AFNetworking 获取完整的 http 响应

标签 ios objective-c cocoa-touch afnetworking

我正在使用最新的 AFNetworking 查询我的服务器。

我的服务器端 PHP 正在发送一个 PHP 相关的错误,但我看不到错误是什么(所以我可以调试它),因为 AFNetworking 需要一个 JSON。

谁能告诉我如何才能看到完整的 HTTP 结果。我研究过,我知道与 success/fail block 中的 operation.responseString 有关,我当前的 block 没有 AFHTTPRequestOperation* 操作,我无法以某种方式将它添加为另一个 block 变量。

非常感谢您的帮助。

下面的代码使用了

NSURL *url = [[NSURL alloc]initWithString:URL];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];


    NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:URLPATH parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
                             {
                                 //...
                                 }

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                                        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
                                                                                            NSLog(@"Inside the success block %@",JSON);
                                                                                            [[self delegate ]shareExerciseDone:JSON];
                                                                                            [[self delegate] activityIndicatorFinish];

                                                                                        }
                                                                                        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){

                                                                                            NSLog(@"Response text is: %@", operation.responseString);
                                                                                            NSLog(@"json text is: %@", JSON);
                                                                                            NSLog(@"Request failed with error: %@, %@", error, error.userInfo);

                                                                                            [[self delegate] activityIndicatorFinish];
                                                                                        }];

    [operation start];

错误

Request failed with error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0xc1b4520 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}, {
    NSDebugDescription = "JSON text did not start with array or object and option to allow fragments not set.";

最佳答案

不要使用AFJSONRequestOperation,只需使用AFHTTPRequestOperation

关于ios - AFNetworking 获取完整的 http 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18385810/

相关文章:

ios - 在哪里以及如何注册接收通知的对象?

objective-c - 从不同着色器导入函数

ios - 不使用 ARC 时替换 __weak

objective-c - 为什么当我测试一个方法抛出异常并且该方法抛出异常时,测试会停止?

ios - "Implicit conversion loses integer precision"同时获取随机数

iphone - 如何找出文件 I/O 操作失败的原因?

objective-c - 如何制作这样的 UIBarButtonItem

ios - 如何检查用户是否已经使用 OneDrive SDK for iOS 登录?

ios - 如何获取用户的时区?

ios - 我怎样才能 swift 改变一个屏幕从另一个屏幕的颜色?