- Consulta à API do Google Books:
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Scanner;
public class GoogleBooksAPI {
public static void main(String[] args) {
HttpClient client = HttpClient.newHttpClient();
Scanner scanner = new Scanner(System.in);
System.out.println("Insira o título do livro:");
String tituloLivro = scanner.nextLine();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://www.googleapis.com/books/v1/volumes?q=" + tituloLivro))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
} catch (Exception e) {
System.out.println("Erro ao fazer a consulta: " + e.getMessage());
}
}
}
- Consulta à API CoinGecko:
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Scanner;
public class CoinGeckoAPI {
public static void main(String[] args) {
HttpClient client = HttpClient.newHttpClient();
Scanner scanner = new Scanner(System.in);
System.out.println("Insira o nome da criptomoeda:");
String criptoMoeda = scanner.nextLine();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.coingecko.com/api/v3/simple/price?ids=" + criptoMoeda + "&vs_currencies=usd"))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
} catch (Exception e) {
System.out.println("Erro ao fazer a consulta: " + e.getMessage());
}
}
}
- Consulta à API TheMealDB:
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Scanner;
public class TheMealDBAPI {
public static void main(String[] args) {
HttpClient client = HttpClient.newHttpClient();
Scanner scanner = new Scanner(System.in);
System.out.println("Insira o nome da receita:");
String nomeReceita = scanner.nextLine();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://www.themealdb.com/api/json/v1/1/search.php?s=" + nomeReceita))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
} catch (Exception e) {
System.out.println("Erro ao fazer a consulta: " + e.getMessage());
}
}
}