2014-01-14 44 views
1

我正试图对宁静服务进行获取请求。当我在没有请求管理器的情况下使用请求时,此请求在过去的三周内一直有效。当我加入了请求管理器,因为我想送的参数字典,代码保持与错误崩溃: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]; 
    */ 
+0

不相关,但如果在请求完成之前有'self'可能被释放的机会,我建议使用'__weak'而不是'__unsafe_unretained',因为后者可能导致'EXC_BAD_ACCESS'。当你的新代码没有使用它们时,你也可以退休'request'和'self.downloadJsonRequest',这只是一个混淆之源。最后,如果你真的需要这个响应是一个可变对象,你可能想像设置'self.downloadJsonRequest.responseSerializer'一样设置'manager.responseSerializer'。 – Rob

回答

1

我建议更换线:

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init]; 

随着

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 

后者调用initWithBaseURL,W这是一些配置的AFHTTPRequestOperationManager,那只叫[[AFHTTPRequestOperationManager alloc] init]似乎并没有做。

如果你仍然有这个问题,我会建议添加Exception Breakpoint来确定这个异常发生的位置。

+1

你是完全正确的,刚刚抓住它2秒前,但你的答案好多了;)谢谢你 –

0

OK,这是一个艰难的一个,但事实证明,requestOperationManager是单身,所以我不应该初始化他,但拨打其背景...