2014-01-14 42 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是單身,所以我不應該初始化他,但撥打其背景...