【发布时间】:2017-09-28 20:35:10
【问题描述】:
我一直在尝试查询 Tesco 的 API 服务。尽管我已经在 Python 上进行了舒适的管理,但我在使用 Objective C 发出请求时遇到了一些麻烦。输出中没有记录任何内容。任何帮助,将不胜感激。程序代码如下:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSURLSessionConfiguration *defaultSessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultSessionConfiguration];
NSURL *url = [NSURL URLWithString:@"https://dev.tescolabs.com/product/?gtin=4548736003446"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString *postParams = @"subscription key=93a6e21eed2e4ca3a858a0f1fc5aaf03";
NSData *postData = [postParams dataUsingEncoding:NSUTF8StringEncoding];
[urlRequest setHTTPMethod:@"GET"];
[urlRequest setHTTPBody:postData];
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Response: %@",response);
NSLog(@"Data: %@",data);
NSLog(@"Error: %@",error);
}];
[dataTask resume];
}
return 0;
}
【问题讨论】:
-
你检查完成处理程序中的错误了吗?
-
是的,我刚刚检查了完成处理程序中的错误,没有记录任何内容。还更新了 URL,这里是 API 的来源:devportal.tescolabs.com/docs/services
-
@Benge 尝试 error.localizedDescription
-
当你说什么都没有被记录时,你的意思是错误是 nil,还是
NSLogs 永远不会被执行? -
NSLog 没有被执行。 @ravi.p 试过了,没有记录错误。