【问题标题】:How to do GET request via AFNetworking?如何通过 AFNetworking 进行 GET 请求?
【发布时间】:2016-04-10 21:36:40
【问题描述】:

我向项目经理添加了目录AFNetworkingUIKit+AFNetworking。 我使用最新的库 3.0.4

在我的类文件.m 中导入文件AFNetworking.h 之后。 我找到了很多如何向 url 发出请求的示例,但是信息很旧。

如何向 URL 发出 GET 请求并从服务器获取数据? 现在函数AFHTTPRequestOperation 已被删除。

这是第一次在 xcode 中使用库,我是初学者。

【问题讨论】:

  • 您不需要第三方库,请查看 NSURLSession。

标签: ios afnetworking-2 afnetworking-3


【解决方案1】:
please try below answer.
+(void)callAFWS:(NSDictionary *)dict withURL:(NSString *)strUrl 
withToken:(NSString *)strToken withBlock:(dictionary)block
{
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

[manager.requestSerializer setValue:strToken forHTTPHeaderField:@"Authorization"];

[manager GET:[NSString stringWithFormat:@"%@/%@",WebserviceUrl,strUrl] parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    if (!responseObject)
    {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        [dict setObject:ServerResponceError forKey:@"error"];
        block(responseObject);
        return ;
    }
    block(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setObject:ServerResponceError forKey:@"error"];
    block(dict);
}];
}

【讨论】:

    【解决方案2】:
    (void)postDatabyAF{
    
    NSString *url;
    
    NSDictionary *dict;
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:
                                  NSJSONReadingAllowFragments];
    
    [manager POST:url parameters:dict success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
    
        NSLog(@" operation.responseData  %@",operation.responseData);
        NSLog(@" operation.response %@",operation.response);
        NSLog(@"statusCode %ld",operation.response.statusCode);
    
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
    
        if (error) {
    
        }
        else{
    
    
        }
    }];
    

    }

    【讨论】:

      【解决方案3】:

      对于 AFNetworking 3.x:

      AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
      [manager GET:@"http://example.com/resources.json" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
          NSLog(@"JSON: %@", responseObject);
      } failure:^(NSURLSessionTask *operation, NSError *error) {
          NSLog(@"Error: %@", error);
      }];
      

      【讨论】:

      • 提问者谈论AFNetworking 3.0+时的最佳答案。 AFNetworking 3.0 不再支持上述其他解决方案。
      【解决方案4】:

      使用 AFNetworking 3.0,您需要如下提出请求:

      NSURL * urlStr = [NSURL URLWithString:strURL];
      
      NSDictionary *dictParameters = @{@"user[height]": height,@"user[weight]": weight};
      
      AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
      
      
      [manager GET:url.absoluteString parameters:dictParameters success:^(NSURLSessionTask *task, id responseObject) {
          NSLog(@"PLIST: %@", responseObject);
      
      } failure:^(NSURLSessionTask *operation, NSError *error) {
          NSLog(@"Error: %@", error);
      
      }];
      

      【讨论】:

        【解决方案5】:
        AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://google.com/"]];
        NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
                                                                path:@"http://google.com/api/pigs/"
                                                          parameters:nil];
        
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        
        [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
        
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        
            // Print the response body in text
            NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
        
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
        }];
        [operation start];
        

        ==============================================

        你也可以使用AFHTTPRequestOperation:

        NSOperationQueue *networkQueue = [[NSOperationQueue alloc] init];
        
        networkQueue.maxConcurrentOperationCount = 5;
        
        NSURL *url = [NSURL URLWithString:@"https://example.com"];
        
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] 
        initWithRequest:request];
        
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
            NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
            NSLog(@"%@", string);
        
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
            NSLog(@"%s: AFHTTPRequestOperation error: %@", __FUNCTION__, error);
        }];
        [networkQueue addOperation:operation];
        

        【讨论】:

        • 非常感谢,但不支持AFHTTPRequestOperation
        • 你只使用#import "AFNetworking.h"吗?编译器看不到对象AFHTTTPClient
        • 请注意,此解决方案在 AFNetworking 3.0 中不起作用,请参阅下面 Chanchal 的回答以获得有效的 sn-p。
        • 为什么在使用 requestWithMethod 时需要 initWithBaseUrl 但还要提供完整路径?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-12
        • 2016-04-05
        相关资源
        最近更新 更多