有没有办法在我的下面的登录代码中打印/ NSLOG API返回的字符串或数组:
-(void)loginToAPI:(NSString *)email password:(NSString *)password {
NSString *controller = @"login";
NSString *action = @"authenticate";
NSDictionary *params = @{@"username": email,
@"userpass": password,
@"controller": controller,
@"action": action,
@"app_id": APP_ID,
@"app_key": APP_KEY};
NSLog(@"params %@", params);
if ([self isNetworkAvailable]) {
AFHTTPRequestOperationManager *client = [AFHTTPRequestOperationManager manager];
client.responseSerializer.acceptableContentTypes = [client.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[client POST:[[Config sharedInstance] getAPIURL]
parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *jsonObject= responseObject;
NSString *status = [jsonObject objectForKey:@"status"];
NSLog(@"Request Successful, response '%@'", jsonObject);
if ([status isEqualToString:@"success"]) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary *data = [jsonObject objectForKey:@"data"];
NSDictionary *userDict = [data objectForKey:@"user"];
NSDictionary *djsaDict = [data objectForKey:@"djsa"];
User *currentUser = [[User alloc] initWithProperties:userDict];
[currentUser save];
DJSA_Cutomer *djsa = [[DJSA_Cutomer alloc] initWithProperties:djsaDict];
NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:currentUser];
NSData *djsaData = [NSKeyedArchiver archivedDataWithRootObject:djsa];
[userDefaults setObject:userData forKey:@"currentUser"];
[userDefaults setObject:djsaData forKey:@"djsa_customer"];
[userDefaults synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName: @"LOGIN_SUCCESS" object:currentUser userInfo:nil];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName: @"LOGIN_FAILED" object:nil userInfo:nil];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Request Failed with Error - loginToAPI: %@, %@", error, error.userInfo);
[[NSNotificationCenter defaultCenter] postNotificationName: @"SERVER_ERROR" object:nil userInfo:nil];
}];
}
}
总是失败并且转到部分"请求失败并出现错误 - loginToAPI"。是否可以查看服务器返回的实际值,以便我可以诊断问题?
谢谢!
答案 0 :(得分:1)
我遇到了同样的问题。在我的案例中,它的解决方案是
client.responseSerializer = [AFHTTPResponseSerializer serializer];
我做到了,但我仍然得到同样的错误。问题是我正在设置client.requestSerializer而不是client.responseSerializer。小错误,但需要很多时间。
答案 1 :(得分:0)
你可以从操作对象中获取响应属性的状态代码,这可以让你知道出了什么问题:
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Request Failed with Error - loginToAPI: %@, %@", error, error.userInfo);
NSLog(@"%@", [NSHTTPURLResponse localizedStringForStatusCode:operation.response.statusCode]);
[[NSNotificationCenter defaultCenter] postNotificationName: @"SERVER_ERROR" object:nil userInfo:nil];
}];
否则,最好使用Charles web proxy之类的工具来检查实际的请求和响应。它有一个免费试用版,应该足以满足您的需要,而且设置相对容易。
答案 2 :(得分:0)
替换此行:
[client.securityPolicy setValidatesDomainName:NO];
[client.securityPolicy setAllowInvalidCertificates:YES];
client.responseSerializer = [AFHTTPResponseSerializer serializer];
用这个:
count()