【问题标题】:AFNetworking fails to create a request on a GET requestAFNetworking 无法在 GET 请求上创建请求
【发布时间】: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 @。您也可以停用 requestself.downloadJsonRequest,因为您的新代码没有使用它们,只是造成混乱。最后,如果您确实需要将响应设为可变对象,您可能需要像设置 self.downloadJsonRequest.responseSerializer 一样设置 manager.responseSerializer

标签: ios objective-c afnetworking afnetworking-2


【解决方案1】:

我建议换行:

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

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

后者调用initWithBaseURL,它对AFHTTPRequestOperationManager进行了一些配置,而仅仅调用[[AFHTTPRequestOperationManager alloc] init]似乎并没有这样做。

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

【讨论】:

  • 你是完全正确的,2秒前刚刚发现,但你的答案要好得多;)谢谢
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多