AFHTTPSessionManager从服务器获取错误响应

时间:2014-02-24 16:37:57

标签: ios afnetworking-2

我正在使用AFNetworking 2,我已经为Session Manager构建了一个单例,除了一件事之外,一切正常,我在用户登录时工作,当用户输入错误的用户名或密码时,服务器响应状态401和json告诉我哪个字段错了。我的问题是,当故障块发生时,我无法得到响应。 NSError只包含标题,但我也想要正文。

API.m

#import "API.h"

#define kAPIHost @"http://myaddress"
#define kAPISignIn @"/signin"

 @implementation API
 @synthesize user;

+ (API *)sharedAPIHTTPClient
{
static API *_sharedAPIHTTPClient = nil;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
    _sharedAPIHTTPClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:kAPIHost]];
});

return _sharedAPIHTTPClient;
}


- (instancetype)initWithBaseURL:(NSURL *)url
{
 self = [super initWithBaseURL:url];

if (self) {

    self.requestSerializer = [AFJSONRequestSerializer serializer];
    self.responseSerializer = [AFJSONResponseSerializer serializer];

}

return self;
}

-(void)SignIn:(NSDictionary*)params {

[self POST:kAPISignIn parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
    if ([self.delegate respondsToSelector:@selector(apiSignInHTTPClient:didSignInSuccesfully:)]) {
        [self.delegate apiSignInHTTPClient:self didSignInSuccesfully:responseObject];
    }
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    if ([self.delegate respondsToSelector:@selector(apiSignInHTTPClient:didFailSignInWithError:)]) {
        [self.delegate apiSignInHTTPClient:self didFailSignInWithError:error];
    }
}];


}



@end

以下是我从LogIn viewController调用它的方法

-(void)logIn
{
NSDictionary *jsonUserDictinary = [NSDictionary dictionaryWithObjectsAndKeys:
                                   @"tester", @"username",
                                   @"123456789", @"password" ,
                                   nil];


NSDictionary *params = @{@"user":jsonUserDictinary};


API *SignInclient = [API sharedAPIHTTPClient];
SignInclient.delegate = self;
[SignInclient SignIn:params];
}

#pragma mark - APIHTTPClientDelegate
-(void)apiSignInHTTPClient:(API *)client didSignInSuccesfully:(id)userData
{
NSLog(@"SUCCESS: %@",userData);
}
-(void)apiSignInHTTPClient:(API *)client didFailSignInWithError:(NSError *)error
{

NSLog(@"Error %@",error);

}

0 个答案:

没有答案