2014-02-06 59 views
2

我正尝试使用位于https://www.mashape.com/ultimate/currency-convert#的mashape的货币转换器API!UNIREST objective c货币转换器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发送的数量,而不是字符串。

感谢您的帮助。

回答

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