我需要你的帮助。我在MaShape上为Metascore找到了一个API,但是我无法让它工作。我使用Cocoapod下载Unirest框架并复制粘贴了Mashape的代码片段
NSDictionary* headers = @{@"X-Mashape-Authorization": @"wZrjWIiAsqdSLGIh3DQzrKoZ5Y3wlo6E"};
NSDictionary* parameters = @{@"title": @"The Elder Scrolls V: Skyrim", @"platform": 1, };
UNIHttpJsonResponse* response = [[UNIRest post:^(UNIBodyRequest* request) {
[request setUrl:@"https://byroredux-metacritic.p.mashape.com/find/game"];
[request setHeaders:headers];
[request setParameters:parameters];
}] asJson];它给了我一堆错误,我把它修正成这样:
NSDictionary* headers = @{@"X-Mashape-Authorization": @"wZrjWIiAsqdSLGIh3DQzrKoZ5Y3wlo6E"};
NSDictionary* parameters = @{@"title": @"The Elder Scrolls V: Skyrim", @"platform": @"1", };
UNIHTTPJsonResponse* response = [[UNIRest post:^(UNISimpleRequest* request) {
[request setUrl:@"https://byroredux-metacritic.p.mashape.com/find/game"];
[request setHeaders:headers];
[request setParameters:parameters];
}] asJson];但是,每当我去调试代码并查看响应内部时,它都是空的,就好像api没有工作一样。你们能告诉我我做错了什么吗?
谢谢
发布于 2014-06-06 22:55:58
您的(固定的)代码片段看起来很好(第一个代码片段确实是错误的),您应该能够像这样打印结果:
UNIHTTPJsonResponse *response = [[UNIRest post:^(UNISimpleRequest *request) {
[request setUrl:@"https://byroredux-metacritic.p.mashape.com/find/game"];
[request setHeaders:headers];
[request setParameters:parameters];
}] asJson];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response.rawBody
options:kNilOptions
error:nil];
NSLog(@"Response status: %ld\n%@", (long) response.code, json);但是,与其执行同步调用,我还建议您切换到异步方式,并检查进程和JSON解析过程中的任何错误:
[[UNIRest post:^(UNISimpleRequest *request) {
[request setUrl:@"https://byroredux-metacritic.p.mashape.com/find/game"];
[request setHeaders:headers];
[request setParameters:parameters];
}] asJsonAsync:^(UNIHTTPJsonResponse* response, NSError *error) {
if (error) {
// Do something with the error
}
NSError *jsonError;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response.rawBody
options:kNilOptions
error:&jsonError];
if (jsonError) {
// Do something with the error
}
NSLog(@"Async response status: %ld\n%@", (long) response.code, json);
// Unirest also provides you this which prevents you from doing the parsing
NSLog(@"%@", response.body.JSONObject);
}];https://stackoverflow.com/questions/24068902
复制相似问题