0

我正在尝试使用 UNIRest API 在 iPhone 应用程序中运行此获取请求

https://api.guildwars2.com/v1/guild_details.json?guild_name=The%20Legacy

我正在运行的代码是这样的

NSDictionary* headers = @{@"accept": @"application/json"};
NSDictionary* parameters = @{@"guild_name": @"The Legacy"};

UNIHTTPJsonResponse* response = [[UNIRest post:^(UNISimpleRequest* request) {
    [request setUrl:@"https://api.guildwars2.com/v1/guild_details.json"];
    [request setHeaders:headers];
    [request setParameters:parameters];
}] asJson];

NSDictionary *guildInformation = response.body.JSONObject;

NSLog(@"response length: %lu", (unsigned long)[guildInformation.allValues count]);

for(NSString *key in [guildInformation allKeys]) {
    NSLog(@"key: %@     object: %@", key, [guildInformation objectForKey:key]);
}

我曾希望 for 循环会显示响应。但是,当您看到唯一的输出是,我似乎根本没有得到任何回应,

response length: 0

我不太了解 UNIRest API 来解决这个问题,也找不到任何好的文档。我究竟做错了什么?

4

1 回答 1

0

问题似乎是参数值未正确编码。

作为一种快速解决方法,您可以简单地传递整个构造的 URL。

UNIHTTPJsonResponse* response = [[UNIRest post:^(UNISimpleRequest* request) {
    [request setUrl:@"https://api.guildwars2.com/v1/guild_details.json?guild_name=The%20Legacy"];
    [request setHeaders:@{@"accept": @"application/json"}];
}] asJson];

可能@“The Legacy”中的空间不会转换为“The%20Legacy”,在将问题添加到https://github.com/Mashape/unirest-obj-c之前会做一个测试用例

更新

只有当我为间隔值添加一个TestCase(它确实可以正常工作)时,我才发现你在使用POST而你应该使用GET。

UNIHTTPJsonResponse* response = [[UNIRest get:^(UNISimpleRequest* request) {
于 2014-05-22T07:40:12.317 回答