【问题标题】:How to make an HTTP request with AFNetorking and NSURLRequest?如何使用 AFNetorking 和 NSURLRequest 发出 HTTP 请求?
【发布时间】:2015-04-01 22:19:18
【问题描述】:

我正在尝试使用 AFHTTPRequestOperationManager 发出 HTTP 请求。我需要使用 AFHTTPRequestOperationManager 因为我希望能够在必要时取消所有操作。由于某种原因,我无法使其正常工作。不调用完成块。我错过了什么吗?

 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://twitter.com/%@", username]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setValue:@"MyUserAgent (iPhone; iOS 7.0.2; gzip)" forHTTPHeaderField:@"User-Agent"];
    [request setHTTPMethod:@"GET"];

    [self.manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        if ([html containsString:@"var h = decodeURI(l.hash.substr(1)).toLowerCase();"]) {
            completion(YES, nil);
        } else {
            completion(NO, nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        completion(NO, error);
    }];

【问题讨论】:

    标签: ios objective-c http afnetworking nsurlrequest


    【解决方案1】:

    这是工作代码,您需要在那里使用 GET 或 POST 方法。

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    NSDictionary *params = @{@"email":emailfield.text};
    
    [manager GET:@"http://example.com/api" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    

    【讨论】:

      【解决方案2】:

      虽然其他人都是对的——你应该使用现代的 AFNetworking 结构而不是旧功能——有一种快速的方法来完成你想要完成的事情。

      从表面上看,- (??? *) HTTPRequestOperationWithRequest:success:failure 方法很可能返回一个AFHTTPRequestOperation。如果我是正确的,您只需要实际开始操作。请参阅下面的代码,已更正。

      NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://twitter.com/%@", username]];
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
      [request setValue:@"MyUserAgent (iPhone; iOS 7.0.2; gzip)" forHTTPHeaderField:@"User-Agent"];
      [request setHTTPMethod:@"GET"];
      
      AFHTTPRequestOperation *op = [self.manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
          NSString *html = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
          if ([html containsString:@"var h = decodeURI(l.hash.substr(1)).toLowerCase();"]) {
              completion(YES, nil);
          } else {
              completion(NO, nil);
          }
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          completion(NO, error);
      }];
      
      [op start];
      

      【讨论】:

      • 我试图以这种奇怪的方式来做这件事,因为我对所有事情都使用了共享管理器,因此如果需要,我可以一次取消所有异步操作,这实际上是我非常需要的功能应用程序。但是,我刚刚决定创建第二个经理,它具有我想要的单独标题字段。谢谢。
      【解决方案3】:

      HTTPRequestOperationWithRequest 方法返回 AFHTTPRequestOperation。您必须将其添加到某个操作队列才能启动它。例如

      AFHTTPRequestOperation *operation = [self.manager HTTPRequestOperationWithRequest:request .........
      [[NSOperationQueue currentQueue] addOperation:operation];
      

      您可以使用比 AFHTTPRequestOperationManager 好一点的 AFHTTPSessionManager 并使用 NSURLSessionDataTask 的方法 cancel 取消请求。你可以在这里找到一些代码示例 - AFNetworking 2.0 cancel specific task

      【讨论】:

        【解决方案4】:

        检查这是否可行

        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", nil];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        
        [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        }];
        

        【讨论】:

          猜你喜欢
          • 2011-05-26
          • 2019-10-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-07-02
          • 1970-01-01
          • 1970-01-01
          • 2021-03-08
          相关资源
          最近更新 更多