Atividade 1: Consulta à API do Google Books
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
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) throws IOException, InterruptedException, URISyntaxException {
Scanner scanner = new Scanner(System.in);
System.out.println("Digite o título do livro:");
String livro = scanner.nextLine();
scanner.close();
// Codificando o título do livro para garantir que caracteres especiais sejam tratados corretamente na URL
String livroEncoded = URLEncoder.encode(livro, "UTF-8");
String url = "https://www.googleapis.com/books/v1/volumes?q=" + livroEncoded;
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Resposta da API: " + response.body());
}
}
Atividade 2: Consulta à API CoinGecko
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
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) throws IOException, InterruptedException, URISyntaxException {
Scanner scanner = new Scanner(System.in);
System.out.println("Digite o nome da criptomoeda: ");
String cripto = scanner.nextLine();
scanner.close();
String criptoEncoded = URLEncoder.encode(cripto, "UTF-8");
String url = "https://api.coingecko.com/api/v3/simple/price?ids=" + criptoEncoded + "&vs_currencies=usd";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
System.out.println("Cotação da criptomoeda: " + response.body());
} else {
System.out.println("Falha ao obter a cotação. Código de status: " + response.statusCode());
}
}
}
Atividade 3: Consulta à API do TheMealDB
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.URISyntaxException;
import java.util.Scanner;
public class TheMealDBAPI {
public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
Scanner scanner = new Scanner(System.in);
System.out.println("Digite o nome da receita:");
String receita = scanner.nextLine();
scanner.close();
String receitaEncoded = URLEncoder.encode(receita, "UTF-8");
String url = "https://www.themealdb.com/api/json/v1/1/search.php?s=" + receitaEncoded;
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Informações sobre a receita: " + response.body());
}
}