📜  exchangerate api - Java (1)

📅  最后修改于: 2023-12-03 15:30:40.596000             🧑  作者: Mango

Exchangerate API - Java

Exchangerate API is a service that provides real-time exchange rates for various currencies. This API can be integrated into Java applications to fetch the latest exchange rates in real-time. This enables developers to create applications with currency conversion features for their users.

Features
  • Up-to-date exchange rates for various currencies
  • Easy integration with Java applications
  • Real-time rate updates
  • Historical exchange rate data
  • Conversion of one currency to another
Getting Started

To use the Exchangerate API in your Java application, you need to follow these steps:

  1. Sign up for an API key at https://www.exchangerate-api.com/
  2. Add the API key to your Java application
  3. Make requests to the Exchangerate API to fetch the latest exchange rates
Add API Key to Your Java Application

To add the API key to your Java application, you can use the following code:

String apiKey = "YOUR_API_KEY";

Replace YOUR_API_KEY with your API key obtained from https://www.exchangerate-api.com/.

Make Requests to the Exchangerate API

To make requests to the Exchangerate API, you can use Java's java.net.HttpURLConnection class. Here is an example code snippet that fetches the latest exchange rate for USD to EUR:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class ExchangerateAPIExample {
  public static void main(String[] args) throws IOException {
    String apiKey = "YOUR_API_KEY";
    URL url = new URL("https://v6.exchangerate-api.com/v6/" + apiKey + "/latest/USD");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    StringBuilder result = new StringBuilder();
    while ((line = rd.readLine()) != null) {
      result.append(line);
    }
    rd.close();
    
    System.out.println(result.toString());
  }
}

This code fetches the latest exchange rate for USD to EUR using the Exchangerate API. Replace YOUR_API_KEY with your API key obtained from https://www.exchangerate-api.com/.

The response from the API will be in JSON format.

Conclusion

The Exchangerate API is a valuable tool for developers who are building applications that require currency conversion. With its easy integration with Java applications, developers can quickly fetch the latest exchange rates and historical data. This API will help developers create powerful and informative applications for their users.