我需要你的帮助。我在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无法正常工作。你们能告诉我我在做什么错吗?
谢谢
最佳答案
您的(固定)代码段看起来不错(第一个确实是错误的),并且您应该能够像这样打印结果:
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);
}];
关于ios - xCode-Mashape API-Unirest,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24068902/