【发布时间】:2014-01-14 14:24:48
【问题描述】:
我正在尝试向安静的服务发出获取请求。当我在没有请求管理器的情况下使用请求时,此请求在过去 3 周内有效。因为我想发送一个参数字典,所以我添加了一个请求管理器后,代码不断崩溃并出现错误:
'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,因为后者可能会导致@987654326 @。您也可以停用request和self.downloadJsonRequest,因为您的新代码没有使用它们,只是造成混乱。最后,如果您确实需要将响应设为可变对象,您可能需要像设置self.downloadJsonRequest.responseSerializer一样设置manager.responseSerializer。
标签: ios objective-c afnetworking afnetworking-2