import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import modelos.Titulo; import modelos.TituloOmdb;
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 PrincipalBuscaAPi { public static void main(String[] args) throws IOException, InterruptedException {
Scanner sc = new Scanner(System.in);
String apiKey = "1d2dc4c6";
// // System.out.println("Digite o Titulo a consultar: "); String buscar = sc.nextLine().replace(" ", "+"); String endereco = "http://www.omdbapi.com/?s="+buscar+"&apikey="+apiKey;
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://www.omdbapi.com/?t=" + buscar + "&apikey=" + apiKey)).build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
String json = response.body();
System.out.println(json);
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
TituloOmdb meuTituloOmdb = gson.fromJson(json, TituloOmdb.class);
System.out.println(meuTituloOmdb);
Titulo meuTitulo = new Titulo(meuTituloOmdb);
System.out.println("----------");
System.out.println(meuTitulo);
}catch (NumberFormatException e){
System.out.println("Não foi possivel converter data");
System.out.println("Messagem: "+ e);
}catch (IllegalArgumentException e){
System.out.println("Verifique o titulo passado na busca.");
System.out.println("Messagem: "+ e);
}catch (Exception e){
System.out.println("Erro desconhecido!");
}finally {
System.out.println("Programa finalizado!");
}
}
}