【问题标题】:xCode - Mashape API - UnirestxCode - Mashape API - Unirest
【发布时间】:2014-07-26 23:03:28
【问题描述】:

我需要你的帮助。我在 MaShape 上为 Metascore 找到了一个 API,但我无法让它工作。 我使用 Cocoapod 下载 Unirest 框架并从 Mashape 复制粘贴代码 sn-p

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 不起作用一样。你们能告诉我我做错了什么吗?

谢谢

【问题讨论】:

    标签: ios objective-c unirest mashape


    【解决方案1】:

    您的(固定)代码 sn-p 看起来不错(第一个确实是错误的),您应该能够像这样打印结果:

    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);
    

    但我也建议你切换到异步方式,而不是同步调用,以及检查过程中的任何错误和J​​SON解析:

    [[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);
    }];
    

    【讨论】:

    • 谢谢你,你是一个救生员。代码有效,谢谢。
    猜你喜欢
    • 2013-11-02
    • 2014-05-01
    • 2014-03-18
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多