gpt4 book ai didi

ios - AFNetworking 500 响应正文

转载 作者:可可西里 更新时间:2023-11-01 03:06:56 26 4
gpt4 key购买 nike

我一直在我的应用程序中使用 AFNetworking 2.0。我注意到,如果我的 Web 服务返回 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);

}];

}

最佳答案

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

答案来自github上的这个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

这是我如何在失败 block 中使用它的示例。

} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",[error.userInfo objectForKey:@"JSONResponseSerializerWithDataKey"]);
}];

关于ios - AFNetworking 500 响应正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19325235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com