AFNetworking 3.0 未获取结果
AFNetworking 3.0 not fetching the results
这是我向服务器发送请求的方式:
- (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
给我时:
Response is: { URL:
http://ts-test01/app/API/ICD10Code/1 } { status code: 200, headers {
"Cache-Control" = "no-cache";
"Content-Length" = 1525;
"Content-Type" = "application/json; charset=utf-8";
Date = "Wed, 05 Oct 2016 09:50:15 GMT";
Expires = "-1";
Pragma = "no-cache";
Server = "Microsoft-IIS/8.0";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET"; } }
但是后端人员正在获取数据。我在发送请求时做错了什么。
你错误地打印了响应,使用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];
这是我向服务器发送请求的方式:
- (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
给我时:
Response is: { URL: http://ts-test01/app/API/ICD10Code/1 } { status code: 200, headers { "Cache-Control" = "no-cache"; "Content-Length" = 1525; "Content-Type" = "application/json; charset=utf-8"; Date = "Wed, 05 Oct 2016 09:50:15 GMT"; Expires = "-1"; Pragma = "no-cache"; Server = "Microsoft-IIS/8.0"; "X-AspNet-Version" = "4.0.30319"; "X-Powered-By" = "ASP.NET"; } }
但是后端人员正在获取数据。我在发送请求时做错了什么。
你错误地打印了响应,使用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];