【问题标题】:AFNetworking 3.0 not fetching the resultsAFNetworking 3.0 未获取结果
【发布时间】:2017-02-13 17:04:58
【问题描述】:

这是我向服务器发送请求的方式:

- (void)searchForText:(NSString *)searchText scope:(NSInteger)scopeOption
{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD showWithStatus:@"Getting Data"];

    NSString *string = [NSString stringWithFormat:@"%@/API/ICD10Code/1",BaseURLString];
    NSDictionary *para = @{@"Code":searchText};

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"PUT" URLString:string parameters:para error:nil];

    NSString *token = [NSString stringWithFormat:@"Bearer %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userToken"]];
    [req setValue:token forHTTPHeaderField:@"Authorization"];

    NSLog(@"URL is: %@ Parameters: %@ Token: %@",string, para, token);

    [[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error){
        if (!error) {
            if (response) {

                NSLog(@"Response is: %@",response);
                [SVProgressHUD dismiss];
            }
        } else {
            // NSLog(@"Error: %@, %@, %@", error, response, responseObject);
            NSLog(@"Error: %@", error.localizedDescription);
            [SVProgressHUD dismiss];
            SCLAlertView *alert = [[SCLAlertView alloc] init];

            [alert showError:self title:@"Error"
                    subTitle:[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"Message"]]
            closeButtonTitle:@"OK" duration:0.0f];
        }
    }] resume];
}

当我运行代码时,NSLog 的响应给了我:

响应是:{ URL: http://ts-test01/app/API/ICD10Code/1 } { 状态码:200,标题 { “缓存控制”=“无缓存”; “内容长度”= 1525; “内容类型”=“应用程序/json;字符集=utf-8”; 日期 =“格林威治标准时间 2016 年 10 月 5 日星期三 09:50:15”; 过期=“-1”; Pragma = "无缓存"; 服务器 = "Microsoft-IIS/8.0"; “X-AspNet-版本”=“4.0.30319”; "X-Powered-By" = "ASP.NET"; } }

但后端人员正在获取数据。我在发送请求时做错了什么。

【问题讨论】:

  • 你是对的

标签: ios objective-c afnetworking-3


【解决方案1】:

您错误地打印了响应,使用responseObject 获取输出

在这个地方

  NSLog(@"Response is: %@",response);

使用

 NSLog(@"output== %@ ", responseObject);

样品

[[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error){
           [SVProgressHUD dismiss];

        if (!error) {
            if (response) {


                 NSLog(@"output== %@", responseObject);

         if ([responseObject isKindOfClass:[NSDictionary class]]) {
          //blah blah
           }
            }
        } else {
            // NSLog(@"Error: %@, %@, %@", error, response, responseObject);
            NSLog(@"Error: %@", error.localizedDescription);
            SCLAlertView *alert = [[SCLAlertView alloc] init];

            [alert showError:self title:@"Error"
                    subTitle:[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"Message"]]
            closeButtonTitle:@"OK" duration:0.0f];
        }
    }] resume];

【讨论】:

  • @TalhaCh - 我回答了你的另一个问题检查一次
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多