gpt4 book ai didi

ios - xCode - Mashape API - Unirest

转载 作者:行者123 更新时间:2023-11-28 20:01:00 25 4
gpt4 key购买 nike

我需要你的帮助。我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com