AFNetworking无法在GET请求上创建请求

时间:2014-01-14 14:24:49

标签: ios objective-c afnetworking afnetworking-2

我正在尝试向宁静的服务提出请求。当我在没有请求管理器的情况下使用请求时,此请求在过去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];
     */

2 个答案:

答案 0 :(得分:1)

我建议更换一行:

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

使用

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

后者调用initWithBaseURL,它执行AFHTTPRequestOperationManager的某些配置,只是调用[[AFHTTPRequestOperationManager alloc] init]似乎没有。

如果您仍然遇到问题,我建议您添加Exception Breakpoint以准确识别此异常发生的位置。

答案 1 :(得分:0)

好吧,这是一个艰难的,但事实证明requestOperationManager是一个单身人士,所以我不应该初始化他,而是调用它的实例......