8

我一直在我的应用程序中使用 AFNetworking 2.0。我注意到,如果我的网络服务返回 500 状态码,我不会得到响应的正文。

这是我的php代码示例

try
{
    $conn = new PDO( "sqlsrv:server=$serverName;Database = $database", $uid, $pwd);
    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    return $conn;
}

catch( PDOException $e )
{
    $response->status(500);
    echo( "Connection Error: " . $e->getMessage() );
}

如果我使用一个简单的休息客户端,这是一个响应主体的示例。

Connection Error: SQLSTATE[08001]: [Microsoft][SQL Server Native Client 11.0]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. 

然而,这似乎是我能从 AFNetworking 得到的唯一回应

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=0x15e58fa0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

这是我的 Objective-c 代码中执行此操作的部分。

...} failure:^(NSURLSessionDataTask *task, NSError *error) {

        NSLog(@"%@",error.description);

    }];

有没有办法获得响应正文?

编辑:更多代码进行澄清

下面是我的 AFHTTPSessionManager 子类的一部分

@implementation MSMAMobileAPIClient

    + (MSMAMobileAPIClient *)sharedClient {
        static MSMAMobileAPIClient *_sharedClient = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _sharedClient = [[MSMAMobileAPIClient alloc] initWithDefaultURL];
        });

        return _sharedClient;
    }

    - (id)initWithDefaultURL {
        return [self initWithBaseURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://%@/mamobile/index.php/" ,[[NSUserDefaults standardUserDefaults] stringForKey:@"serviceIPAddress"]]]];
    }

    - (id)initWithBaseURL:(NSURL *)url {

        self = [super initWithBaseURL:url];
        if (!self) {
            return nil;
        }
        self.responseSerializer = [AFCompoundResponseSerializer compoundSerializerWithResponseSerializers:@[[AFJSONResponseSerializer serializer], [AFHTTPResponseSerializer serializer]]];

        return self;
    }

我尝试将响应序列化程序设置为 AFCompoundResponseSerializer 但似乎没有什么区别

下面是一个我称之为图书馆员的子类的例子。

-(void)searchForItemWithString:(NSString *)searchString withCompletionBlock:(arrayBlock)block {

    self.inventorySearchBlock = block;

    NSDictionary *parameters = @{@"query": searchString};

    [[MSMAMobileAPIClient sharedClient] GET:@"inventory/search" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {

        if (!responseObject) {
            NSLog(@"Error parsing JSON");
        } else {
            //do stuff with the json dictionary that's returned..
        }

    } failure:^(NSURLSessionDataTask *task, NSError *error) {

        NSLog(@"Error: %@",error.description);

    }];

}
4

3 回答 3

14

更新:我创建了一个 github 存储库来包含我正在使用的最新代码。所有更改都将在此处发布。https://github.com/Hackmodford/HMFJSONResponseSerializerWithData

答案来自github上的this issue。 https://github.com/AFNetworking/AFNetworking/issues/1397

gfiumara 是提出这个问题的开发者。我只是稍微修改了他的 AFJSONResponseSerializer 子类以包含一个实际的字符串而不是 NSData

//MSJSONResponseSerializerWithData.h

#import "AFURLResponseSerialization.h"

/// NSError userInfo key that will contain response data
static NSString * const JSONResponseSerializerWithDataKey = @"JSONResponseSerializerWithDataKey";

@interface MSJSONResponseSerializerWithData : AFJSONResponseSerializer

@end

//  MSJSONResponseSerializerWithData.m

#import "MSJSONResponseSerializerWithData.h"

@implementation MSJSONResponseSerializerWithData

- (id)responseObjectForResponse:(NSURLResponse *)response
                           data:(NSData *)data
                          error:(NSError *__autoreleasing *)error
{
    if (![self validateResponse:(NSHTTPURLResponse *)response data:data error:error]) {
        if (*error != nil) {
            NSMutableDictionary *userInfo = [(*error).userInfo mutableCopy];
            userInfo[JSONResponseSerializerWithDataKey] = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSError *newError = [NSError errorWithDomain:(*error).domain code:(*error).code userInfo:userInfo];
            (*error) = newError;
        }

        return (nil);
    }

    return ([super responseObjectForResponse:response data:data error:error]);
}

@end

这是我如何在故障块中使用它的示例。

} failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@",[error.userInfo objectForKey:@"JSONResponseSerializerWithDataKey"]); 
    }];
于 2013-10-15T14:15:16.990 回答
0

你需要用来AFCompoundSerializer告诉 AFNetworking 框架如何处理它可能收到的所有可能的响应。默认情况下,它只会尝试映射 JSON。复合序列化程序将通过序列化程序工作,直到找到不引发错误的序列化程序。


你想使用:

+ (instancetype)compoundSerializerWithResponseSerializers:(NSArray *)responseSerializers

AFCompoundResponseSerializer(中AFURLResponseSerialization.h)。

您需要传递一组可以处理响应的序列化程序。数组中的序列化程序之一应该是AFHTTPResponseSerializer处理错误响应的实例。

于 2013-10-11T21:22:08.280 回答
0

如果你在你的项目中包含我的类别,那么简单如下:

[mySessionManager POST:@"some-api" parameters:params success:^(NSURLSessionDataTask *task, NSDictionary *responseObject) {
    ...
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    id responseObject = error.userInfo[kErrorResponseObjectKey];
    ... do something with the response ...
}];

这是我的类别的代码。它调动 AFURLSessionManager 将 shim 注入到完成处理程序中。垫片将响应放入 NSError 的 userInfo 中。

https://gist.github.com/chrishulbert/35ecbec4b37d36b0d608

于 2014-09-09T04:25:39.060 回答