|
| 1 | +import com.google.gson.JsonArray; |
| 2 | +import com.google.gson.JsonElement; |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.google.gson.JsonParser; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.URI; |
| 8 | +import java.net.http.HttpClient; |
| 9 | +import java.net.http.HttpRequest; |
| 10 | +import java.net.http.HttpResponse; |
| 11 | +import java.net.http.HttpResponse.BodyHandlers; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Scanner; |
| 15 | + |
| 16 | +public class Josegs95 { |
| 17 | + public static void main(String[] args) { |
| 18 | + //Ejercicio |
| 19 | + HttpClient client = HttpClient.newHttpClient(); |
| 20 | + HttpRequest request = HttpRequest.newBuilder() |
| 21 | + .uri(URI.create("https://www.google.es")) |
| 22 | + .GET() |
| 23 | + .build(); |
| 24 | + try { |
| 25 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 26 | + //Si no salta excepción, la petición fue exitosa. |
| 27 | + if (response.statusCode() != 200) |
| 28 | + System.out.println("Ha habido algún problema con la conexión. Cod: " + response.statusCode()); |
| 29 | + else{ |
| 30 | + System.out.println("La conexión ha sido un éxito."); |
| 31 | + System.out.println(response.body()); |
| 32 | + } |
| 33 | + } catch (IOException e) { |
| 34 | + throw new RuntimeException(e); |
| 35 | + } catch (InterruptedException e) { |
| 36 | + throw new RuntimeException(e); |
| 37 | + } |
| 38 | + |
| 39 | + //Reto |
| 40 | + retoFinal(); |
| 41 | + } |
| 42 | + |
| 43 | + public static void retoFinal(){ |
| 44 | + //Voy a escribirlo todo en un método para hacerlo más sencillo y porque ya existen librerías especializadas |
| 45 | + //en trabajar con la pokeapi. |
| 46 | + |
| 47 | + Scanner sc = new Scanner(System.in); |
| 48 | + System.out.print("Introduzca el nombre o el número del Pokémon: "); |
| 49 | + String pokemon = sc.nextLine().toLowerCase(); |
| 50 | + |
| 51 | + HttpClient client = HttpClient.newHttpClient(); |
| 52 | + HttpRequest request = HttpRequest.newBuilder() |
| 53 | + .uri(URI.create("https://pokeapi.co/api/v2/pokemon/" + pokemon)) |
| 54 | + .build(); |
| 55 | + try { |
| 56 | + HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); |
| 57 | + |
| 58 | + if(response.statusCode() != 200) |
| 59 | + System.out.println("Error " + response.statusCode() + ". Pokémon desconocido."); |
| 60 | + else{ |
| 61 | + JsonObject pokemonData = JsonParser.parseString(response.body()).getAsJsonObject(); |
| 62 | + |
| 63 | + //ID, nombre, peso, altura y tipos |
| 64 | + String id = pokemonData.get("id").getAsString(); |
| 65 | + String name = pokemonData.get("name").getAsString(); |
| 66 | + double weight = pokemonData.get("weight").getAsDouble() / 10.0; |
| 67 | + double height = pokemonData.get("height").getAsDouble() / 10.0; |
| 68 | + |
| 69 | + List<String> typeList = new ArrayList<>(); |
| 70 | + for (JsonElement type : pokemonData.get("types").getAsJsonArray()) { |
| 71 | + JsonObject aux = type.getAsJsonObject(); |
| 72 | + typeList.add(aux.get("type").getAsJsonObject().get("name").getAsString()); |
| 73 | + } |
| 74 | + |
| 75 | + System.out.println("ID: " + id); |
| 76 | + System.out.println("Nombre: " + name); |
| 77 | + System.out.println("Peso: " + weight + " kg."); |
| 78 | + System.out.println("Altura: " + height + " m."); |
| 79 | + System.out.print("Tipos: "); |
| 80 | + for (String type : typeList) |
| 81 | + System.out.print(type + " "); |
| 82 | + System.out.println(); |
| 83 | + |
| 84 | + //Evoluciones |
| 85 | + request = HttpRequest.newBuilder(URI.create("https://pokeapi.co/api/v2/pokemon-species/" + id)) |
| 86 | + .build(); |
| 87 | + response = client.send(request, BodyHandlers.ofString()); |
| 88 | + |
| 89 | + if (response.statusCode() == 200){ |
| 90 | + JsonObject specieData = JsonParser.parseString(response.body()).getAsJsonObject(); |
| 91 | + String evoChainURL = specieData.get("evolution_chain").getAsJsonObject().get("url").getAsString(); |
| 92 | + |
| 93 | + request = HttpRequest.newBuilder(URI.create(evoChainURL)) |
| 94 | + .build(); |
| 95 | + response = client.send(request, BodyHandlers.ofString()); |
| 96 | + |
| 97 | + if (response.statusCode() == 200){ |
| 98 | + JsonObject evolutionData = JsonParser.parseString(response.body()).getAsJsonObject(); |
| 99 | + JsonElement auxElement = evolutionData.get("chain"); |
| 100 | + List<String> evolutionList = new ArrayList<>(); |
| 101 | + String basePokemonName = auxElement.getAsJsonObject() |
| 102 | + .get("species").getAsJsonObject() |
| 103 | + .get("name").getAsString(); |
| 104 | + evolutionList.add(basePokemonName); |
| 105 | + for(JsonElement evolution : auxElement.getAsJsonObject().get("evolves_to").getAsJsonArray()){ |
| 106 | + String aux = evolution.getAsJsonObject().get("species").getAsJsonObject().get("name").getAsString(); |
| 107 | + evolutionList.add(aux); |
| 108 | + for (JsonElement evolution2 : evolution.getAsJsonObject().get("evolves_to").getAsJsonArray()){ |
| 109 | + aux = evolution2.getAsJsonObject().get("species").getAsJsonObject().get("name").getAsString(); |
| 110 | + evolutionList.add(aux); |
| 111 | + } |
| 112 | + } |
| 113 | + System.out.println("Línea evolutiva: " + evolutionList); |
| 114 | + //Juegos |
| 115 | + List<String> gameList = new ArrayList<>(); |
| 116 | + for (JsonElement game : pokemonData.get("game_indices").getAsJsonArray()) |
| 117 | + gameList.add(game.getAsJsonObject() |
| 118 | + .get("version").getAsJsonObject() |
| 119 | + .get("name").getAsString()); |
| 120 | + System.out.print("Juegos: "); |
| 121 | + for (String gameName : gameList) |
| 122 | + System.out.print("[Pokémon " + gameName + "] "); |
| 123 | + System.out.println(); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } catch (IOException e) { |
| 128 | + throw new RuntimeException(e); |
| 129 | + } catch (InterruptedException e) { |
| 130 | + throw new RuntimeException(e); |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | +} |
0 commit comments