我在保存来自POST请求的响应时遇到问题。 基于AFnetworking documentation和NSScreencast tutorial我创建了自己的AFHTTPRequestOperationManager子类,但我不确定为什么没有保存响应。
我怎么知道,答案没有保存?
因为控制台中的there is an error:(null)
消息和我的方法不执行segue。
我知道我正在获取值,因为我放置的断点NSURLSessionDataTask
但我不知道为什么这些值没有保存,我收到一条错误信息。我感谢任何帮助。
APIClient /经理
AuthAPIManager.h
#import "AFHTTPSessionManager.h"
@interface AuthAPIManager : AFHTTPSessionManager
+(AuthAPIManager *)sharedManager;
-(NSURLSessionDataTask *)initializeLogin:(NSString *)username completion:(void(^)(NSDictionary *results, NSError *error))completion;
//for login
@property (nonatomic,readonly,retain)NSString *StoreIdentifierForVendor;
@property(nonatomic,copy)NSString *devicetype;
@end
AuthAPIManager.m
#import "AuthAPIManager.h"
#import "LoginInfo.h"
#import "CredentialStore.h"
static AuthAPIManager *sharedManager =nil;
static dispatch_once_t onceToken;
@implementation AuthAPIManager
+(AuthAPIManager *)sharedManager
{
dispatch_once(&onceToken, ^{
sharedManager = [[AuthAPIManager alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL]];
sharedManager.responseSerializer=[AFJSONResponseSerializer serializer];
sharedManager.requestSerializer = [AFJSONRequestSerializer serializer];
});
return sharedManager;
}
-(id)initWithBaseURL:(NSURL *)url
{
self = [super initWithBaseURL:url];
if (self) {
}
return self;
}
-(NSURLSessionDataTask *)initializeLogin:(NSString *)username completion:(void (^)(NSDictionary *, NSError *))completion
{
_devicetype = @"ios";
_StoreIdentifierForVendor = [[[UIDevice currentDevice]identifierForVendor]UUIDString];
id loginParameters =@{@"AccountId":username,
@"DeviceType":_devicetype
};
NSURLSessionDataTask *task =[self POST:@"/Accn" parameters:loginParameters
success:^(NSURLSessionDataTask *task, id responseObject)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
if (httpResponse.statusCode == 200) {
LoginInfo *loginInfo =[[LoginInfo alloc]initWithDictionary:responseObject];
CredentialStore *credStore =[CredentialStore sharedStore];
credStore.loginInfo =loginInfo;
completion(responseObject,nil);
loginInfo = responseObject;
} else {
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, nil);
});
NSLog(@"Received: %@", responseObject);
NSLog(@"Received HTTP %d", httpResponse.statusCode);
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, error);
});
}];
return task;
}
@end
这就是我在视图控制器中调用方法的方法
- (IBAction)login:(id)sender
{
[_usernameTextField resignFirstResponder];
[SVProgressHUD show];
NSURLSessionDataTask *task = [[AuthAPIManager sharedManager] initializeLogin:self.usernameTextField.text completion:^(NSDictionary *results, NSError *error)
{
if (results) {
LoginInfo *loginInfo = [[LoginInfo alloc]initWithDictionary:results];
CredentialStore *credStore =[ CredentialStore sharedStore];
credStore.loginInfo =loginInfo;
[self performSegueWithIdentifier:@"welcomeViewSegue" sender:self];
}
else
{
NSLog(@"there is an error:%@",error);
}
}];
[SVProgressHUD dismiss];
}
答案 0 :(得分:0)
正在调用您的成功阻止,但您的状态代码不是200。
你正在调用你的完成块:
completion(nil, nil);
所以这段代码:
if (results) {
[snip]
}
else
{
NSLog(@"there is an error:%@",error);
}
传递nil
结果对象。
if (httpResponse.statusCode == 200)
上设置断点并检查调试器中的httpResponse
,看看为什么它不符合您的预期。 (您可能会获得不同的成功代码,例如204
。)nil
调用完成功能块。