我正试图对宁静服务进行获取请求。当我在没有请求管理器的情况下使用请求时,此请求在过去的三周内一直有效。当我加入了请求管理器,因为我想送的参数字典,代码保持与错误崩溃:AFNetworking无法在GET请求上创建请求
'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: urlRequest'
我的代码如下。如果我取消注释并用它替换新代码,注释掉的代码仍然有效。为什么我能够通过该URL进行请求,但不能与AFNetworking进行请求?
NSString *URLForSend = [NSString stringWithFormat:@"%@%@/", ([_remoteDownloadURLString stringByAppendingString:_getFilesJsonURLString]),_userName ];
NSURL *URL = [NSURL URLWithString:URLForSend];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
self.downloadJsonRequest = [[AFHTTPRequestOperation alloc] initWithRequest:request];//this works to create a a afhttprequestoperation, with the same url
_downloadJsonRequest.responseSerializer =
[AFJSONResponseSerializer serializerWithReadingOptions: NSJSONReadingMutableContainers];
__unsafe_unretained typeof(self) weakSelf = self;
NSMutableDictionary *mutDictForSend = [[NSMutableDictionary alloc] initWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:@"tokenInfo"]];
[mutDictForSend removeObjectForKey:@"success"];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
[manager GET:URLForSend parameters:mutDictForSend success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"%@", responseObject);
weakSelf.jsonArrayForCaching = responseObject;
[[NSNotificationCenter defaultCenter] postNotificationName:kBIDContentEndingSync object:weakSelf];
[weakSelf gotNewJson:responseObject];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Request Failure Because %@",[error userInfo]);
}];
/*
[_downloadJsonRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"%@", responseObject);
weakSelf.jsonArrayForCaching = responseObject;
[[NSNotificationCenter defaultCenter] postNotificationName:kBIDContentEndingSync object:weakSelf];
[weakSelf gotNewJson:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Request Failure Because %@",[error userInfo]);
}];
[[NSNotificationCenter defaultCenter] postNotificationName:kBIDContentStartingSync object:self];
[_downloadJsonRequest start];
*/
不相关,但如果在请求完成之前有'self'可能被释放的机会,我建议使用'__weak'而不是'__unsafe_unretained',因为后者可能导致'EXC_BAD_ACCESS'。当你的新代码没有使用它们时,你也可以退休'request'和'self.downloadJsonRequest',这只是一个混淆之源。最后,如果你真的需要这个响应是一个可变对象,你可能想像设置'self.downloadJsonRequest.responseSerializer'一样设置'manager.responseSerializer'。 – Rob