Hac*_*ord 8 rest objective-c ios afnetworking-2
我一直在我的应用程序中使用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() );
}
Run Code Online (Sandbox Code Playgroud)
如果我使用简单的休息客户端,这是一个响应体的示例.
Connection Error: SQLSTATE[08001]: [Microsoft][SQL Server Native Client 11.0]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
Run Code Online (Sandbox Code Playgroud)
然而,这似乎是我从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.}
Run Code Online (Sandbox Code Playgroud)
这是我的Objective-c代码的一部分.
...} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error.description);
}];
Run Code Online (Sandbox Code Playgroud)
有没有办法让我得到反应机构?
编辑:更多代码以便澄清
下面是我的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;
}
Run Code Online (Sandbox Code Playgroud)
我尝试将响应序列化器设置为AFCompoundResponseSerializer但它似乎没有什么区别
下面是我称之为Librarian的子类的示例.
-(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);
}];
}
Run Code Online (Sandbox Code Playgroud)
Hac*_*ord 14
更新:我已经创建了一个github存储库来包含我正在使用的最新代码.所有更改都将在那里发布.https://github.com/Hackmodford/HMFJSONResponseSerializerWithData
答案来自github上的这个问题. 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
Run Code Online (Sandbox Code Playgroud)
// 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
Run Code Online (Sandbox Code Playgroud)
这是我在故障块中如何使用它的示例.
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",[error.userInfo objectForKey:@"JSONResponseSerializerWithDataKey"]);
}];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7491 次 |
| 最近记录: |