1 - Crie um programa em Java que utilize as classes HttpClient, HttpRequest e HttpResponse para fazer uma consulta à API do Google Books. Solicite ao usuário que insira o título de um livro, e exiba as informações disponíveis sobre o livro retornado pela API.
package Challenges.exercise01;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Scanner;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
public class Service {
Scanner scanner = new Scanner(System.in);
Gson gson = new Gson();
private String title = scanner.nextLine();
private String apiKey = "";
public String getTitle() {
return title;
}
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://www.googleapis.com/books/v1/volumes?q=" + title + "&maxResults=1&key=" + apiKey))
.build();
HttpResponse<String> response;
{
try {
response = client.send(request, HttpResponse.BodyHandlers.ofString());
String responseBody = response.body();
JsonObject jsonResponse = gson.fromJson(responseBody, JsonObject.class);
JsonArray items = jsonResponse.getAsJsonArray("items");
for (int i = 0; i < items.size(); i++) {
JsonObject item = items.get(i).getAsJsonObject();
JsonObject volumeInfo = item.getAsJsonObject("volumeInfo");
String title = volumeInfo.get("title").getAsString();
String authors = volumeInfo.getAsJsonArray("authors").toString();
System.out.println("Title: " + title + ", Authors: " + authors);
}
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
package Challenges.exercise01;
public class Main {
public static void main(String[] args) {
System.out.println("Enter the name of the book you want to search for");
Service service = new Service();
service.getTitle();
}
}
Saída esperada:
Enter the name of the book you want to search for
Starwars
Title: STAR WARS - Herdeiro do Império, Authors: ["Timothy Zahn"]
2 - Crie um programa Java que utiliza as classes HttpClient, HttpRequest e HttpResponse para fazer uma consulta à API CoinGecko e exiba a cotação atual de uma criptomoeda escolhida pelo usuário.
package Challenges.exercise02;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.io.IOException;
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 Service {
Scanner scanner = new Scanner(System.in);
Gson gson = new Gson();
private String criptoName = scanner.nextLine();
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.coingecko.com/api/v3/simple/price?ids=" +criptoName+ "&vs_currencies=brl"))
.build();
HttpResponse<String> response;
{
try {
response = client.send(request, HttpResponse.BodyHandlers.ofString());
String responseBody = response.body();
JsonObject jsonResponse = gson.fromJson(responseBody, JsonObject.class);
JsonObject criptoObject = jsonResponse.getAsJsonObject(criptoName);
if (criptoObject != null) {
// Acessa o valor em reais
String value = criptoObject.get("brl").getAsString();
System.out.println("Crypto: " +criptoName+
"\n Value: " +value);
} else {
System.out.println("Crypto not found: " + criptoName);
}
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
package Challenges.exercise02;
public class Main {
public static void main(String[] args) {
System.out.println("Enter the cryptocurrency name to see the quote");
var service = new Service();
}
}
Saída esperada:
Enter the cryptocurrency name to see the quote
bitcoin
Crypto: bitcoin
Value: 347994