我正在尝试使用AFNetworking发布JSON。
以下是我使用的代码:
+ (RESTAPI *)sharedClient
{
static RESTAPI *_sharedClient = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:@"https://mybaseurl.com"]];
});
return _sharedClient;
}
- (id)initWithBaseURL:(NSURL *)url
{
self = [super initWithBaseURL:url];
if (!self) {
return nil;
}
[self setParameterEncoding:AFJSONParameterEncoding];
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setDefaultHeader:@"Accept" value:@"application/json"];
[self setAllowsInvalidSSLCertificate:YES];
return self;
}
以下代码不起作用。每次我尝试时,都会收到以下错误:
无法完成操作。(NSURLErrorDomain error -1012。
// this code does not works
//
- (void)loginNOTWORKING
{
RESTAPI *client = [RESTAPI sharedClient];
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
[[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];
NSDictionary *parameter = @{@"tgout": @"1",
@"tgin": @2,
@"username": @"foo",
@"password":@"bar"};
NSURLRequest *request = [client requestWithMethod:@"POST" path:@"/login" parameters:parameter];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// code for successful return goes here
[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
NSLog(@"THIS IS NEVER CALLED: %@", JSON);
// do something with return data
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
// code for failed request goes here
[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
NSLog(@"SAD, VERY SAD: %@", error.localizedDescription);
// do something on failure
}];
[operation start];
}
此代码有效:
// this code WORKS
- (void)loginWORKING
{
RESTAPI *client = [RESTAPI sharedClient];
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
[[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];
NSDictionary *parameter = @{@"tgout": @"1",
@"tgin": @2,
@"username": @"foo",
@"password":@"bar"};
[client postPath:@"/login" parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Print the response body in text
NSLog(@"IT WORKS: %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Response: %@", error.localizedDescription);
}];
}
为什么第一种登录方法不起作用?我做错了什么?
尝试通过替换
NSURLRequest *request = [client requestWithMethod:@"POST" path:@"/login" parameters:parameter];
跟
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/*HERE THE URL STRING TO CALL*/"]]
您可以在文件 CFNetworkErrors.h
中找到错误 -1012 :
kCFURLErrorUserCancelledAuthentication = -1012
"The connection failed because the user cancelled required authentication."
我想,您的身份验证存在问题。错误描述可能在"用户"方面具有误导性 - 它实际上是一个被调用的委托方法,它取消了身份验证,或者身份验证只是失败。
当然,这可能是由于未正确序列化参数而导致的。我建议使用较低级别的API,手动创建请求,使用NSJSONSerialization
手动对JSON进行编码,并设置请求的正文数据和URL。恕我直言,这当然是更具可读性的代码,并且可能需要更少的代码。