我正試圖對寧靜服務進行獲取請求。當我在沒有請求管理器的情況下使用請求時,此請求在過去的三週內一直有效。當我加入了請求管理器,因爲我想送的參數字典,代碼保持與錯誤崩潰: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