Intégration d'une API pour un taux de change en temps réel.
Bonjour !
Ceci est mon premier billet ! Je suis étudiant en science informatique de niveau Collège.
Je suis en train de coder en java sous Netbeans une classe qui permet d'obtenir en temps réel les taux de change concernant les devises. Pour ce faire j'utilise l'API : https://currencylayer.com/ en mode Gratuit. Toutefois, je n'arrive pas à implémenter l'API correctement dans la classe. Voici la classe avec un API tierce : free.currencyconverterapi.com qui a été discontinué. Je tente de le substituer avec l'api de currencylayer qui est en gras toutefois sans succès. Je met le code en référence au bas de la page.
Voilà
Merci d'avance pour vos réponses.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class BanquedeMonaco
{
public static void main(String args[]) {
// converting 1000 Euro to US Dollar
System.out.println("Euro/US Dollar: " + findExchangeRateAndConvert("CAD", "EURO", 1000));
// converting 1000 US Dollar to Euro
System.out.println("US Dollar/Euro: " + findExchangeRateAndConvert("CAD", "USD", 1000));
// converting 1000 US Dollar to Indian Rupee
System.out.println("US Dollar/Indian Rupee: " + findExchangeRateAndConvert("CHF", "EURO", 1000));
// converting 1000 Indian Rupee to US Dollar
System.out.println("Indian Rupee/US Dollar: " + findExchangeRateAndConvert("GBD", "USD", 1000));
}
private static Double findExchangeRateAndConvert(String from, String to, int amount) {
try {
//Yahoo Finance API
URL url2 = new URL("http://free.currencyconverterapi.com/api/v5/convert?q=CAD_EUR&compact=y"+ from + to + "=X");
URL url3 = new URL("http://free.currencyconverterapi.com/api/v5/convert?q=USD_EUR&compact=y"+ from + to + "=X");
URL url4 = new URL("http://free.currencyconverterapi.com/api/v5/convert?q=USD_YEN&compact=y"+ from + to + "=X");
URL url = new URL("http://apilayer.net/api/live?access_key = 36638440b3ca2bb82a5a6cbab38d98ca& source = GBP" + from + to + "=X") )
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line = reader.readLine();
if (line.length() > 0) {
return Double.parseDouble(line) * amount;
}
reader.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
} |