UNIRest Objective-C 货币转换器 API

问题描述 投票:0回答:1

我正在尝试使用位于 https://www.mashape.com/ultimate/currency-convert#!

的 mashape 货币转换器 API

我是 Objective-C 新手。我正在尝试通过此代码调用 API -

NSDictionary* headers = @{@"X-Mashape-Authorization": @"key"};
NSDictionary* parameters = @{@"amt": @"2", @"from": @"USD", @"to": @"INR", @"accuracy": @"2"};

UNIHTTPJsonResponse* response = [[UNIRest post:^(UNISimpleRequest* request) {
    [request setUrl:@"https://exchange.p.mashape.com/exchange/?amt=120&from=usd&to=gbp&accuracy=3&format=json"];
    [request setHeaders:headers];
    [request setParameters:parameters];
}] asJson];

有人可以告诉我如何访问返回的信息以及如何将参数 2 作为数字而不是字符串发送。

ios json objective-c nsdictionary
1个回答
2
投票

mashape 的 API 似乎并未全部标准化到从参数数组中获取参数 - 您需要在 UNIHTTPJsonResponse 对象的 setUrl 调用中传递它们。

此外,在从远程 API 获取数据时使用异步调用是一个好主意。

    NSDictionary* headers = @{@"X-Mashape-Authorization": @"key"};


[[UNIRest post:^(UNISimpleRequest* request) {
    [request setUrl:@"https://exchange.p.mashape.com/exchange/?amt=120&from=usd&to=gbp&accuracy=3&format=json"]; // this is where you want to set your currencies, amounts, etc. 
    [request setHeaders:headers];
    [request setParameters:@{}]; // is this needed? I dunno
}] asJsonAsync:^(UNIHTTPJsonResponse* response, NSError *error) {
    if (error) {
        NSLog(@"%@",[error localizedDescription]);
    } else {

        // here you do all the stuff you want to do with the data you got.
        // like launch any code that actually deals with the data :)

        NSDictionary *currencyResult = [NSJSONSerialization JSONObjectWithData:[response rawBody] options: 0 error: &error];
        NSLog(@"%@", currencyResult);
    }
}];
© www.soinside.com 2019 - 2024. All rights reserved.