import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.io.IOException; import org.json.JSONObject;
public class APIConsultas { public static void main(String[] args) { // Consulta à API do Google Books try { HttpClient client = HttpClient.newHttpClient(); System.out.println("Digite o título do livro: "); java.util.Scanner scanner = new java.util.Scanner(System.in); String bookTitle = scanner.nextLine(); String apiUrl = "https://www.googleapis.com/books/v1/volumes?q=" + bookTitle; HttpRequest request = HttpRequest.newBuilder().uri(URI.create(apiUrl)).build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); if (response.statusCode() == 200) { String responseBody = response.body(); JSONObject jsonResponse = new JSONObject(responseBody); if (jsonResponse.getJSONArray("items").length() > 0) { JSONObject bookInfo = jsonResponse.getJSONArray("items").getJSONObject(0).getJSONObject("volumeInfo"); System.out.println("Título: " + bookInfo.getString("title")); System.out.println("Autor(es): " + bookInfo.getJSONArray("authors").join(", ")); System.out.println("Descrição: " + bookInfo.optString("description", "Não disponível")); } else { System.out.println("Livro não encontrado."); } } else { System.out.println("Erro ao consultar a API: " + response.statusCode()); }
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
// Consulta à API CoinGecko
try {
HttpClient client = HttpClient.newHttpClient();
System.out.println("Digite o nome da criptomoeda (ex: bitcoin, ethereum): ");
java.util.Scanner scanner = new java.util.Scanner(System.in);
String cryptoName = scanner.nextLine();
String apiUrl = "https://api.coingecko.com/api/v3/simple/price?ids=" + cryptoName + "&vs_currencies=brl";
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(apiUrl)).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
String responseBody = response.body();
JSONObject jsonResponse = new JSONObject(responseBody);
if (jsonResponse.has(cryptoName)) {
double price = jsonResponse.getJSONObject(cryptoName).getDouble("brl");
System.out.println("Preço atual de " + cryptoName + " em reais: R$ " + price);
} else {
System.out.println("Criptomoeda não encontrada.");
}
} else {
System.out.println("Erro ao consultar a API: " + response.statusCode());
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
// Consulta à API do TheMealDB
try {
HttpClient client = HttpClient.newHttpClient();
System.out.println("Digite o nome da receita: ");
java.util.Scanner scanner = new java.util.Scanner(System.in);
String recipeName = scanner.nextLine();
String apiUrl = "https://www.themealdb.com/api/json/v1/1/search.php?s=" + recipeName;
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(apiUrl)).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
String responseBody = response.body();
JSONObject jsonResponse = new JSONObject(responseBody);
if (jsonResponse.getJSONArray("meals").length() > 0) {
JSONObject mealInfo = jsonResponse.getJSONArray("meals").getJSONObject(0);
System.out.println("Nome da receita: " + mealInfo.getString("strMeal"));
System.out.println("Categoria: " + mealInfo.getString("strCategory"));
System.out.println("Instruções: " + mealInfo.getString("strInstructions"));
} else {
System.out.println("Receita não encontrada.");
}
} else {
System.out.println("Erro ao consultar a API: " + response.statusCode());
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
} Esse código integra as três consultas de APIs (Google Books, CoinGecko e TheMealDB) em um único programa Java.