Skip to content

Commit 13a16be

Browse files
authored
Merge pull request mouredev#4448 from Abel-ADE/main
#20 - Java
2 parents dea9c77 + 5fab0fa commit 13a16be

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
<dependencies>
3+
<dependency>
4+
<groupId>com.google.code.gson</groupId>
5+
<artifactId>gson</artifactId>
6+
<version>2.10.1</version>
7+
</dependency>
8+
</dependencies>
9+
*/
10+
11+
import com.google.gson.Gson;
12+
import com.google.gson.JsonArray;
13+
import com.google.gson.JsonElement;
14+
import com.google.gson.JsonObject;
15+
16+
import java.io.IOException;
17+
import java.net.HttpURLConnection;
18+
import java.net.URL;
19+
import java.util.ArrayList;
20+
import java.util.Scanner;
21+
22+
public class Main {
23+
public final static ArrayList<String> nameEvolves = new ArrayList<>();
24+
25+
public static void getEvolve(JsonObject json){
26+
String nameEvolve = json.getAsJsonObject("species").get("name").getAsString();
27+
nameEvolves.add(nameEvolve);
28+
29+
JsonArray evolves = json.getAsJsonArray("evolves_to");
30+
if(evolves.size() > 0){
31+
getEvolve(evolves.get(0).getAsJsonObject());
32+
}
33+
}
34+
35+
public static void main(String[] args) {
36+
try {
37+
System.out.println("Primera petición HTTP");
38+
39+
//Establecemos la conexón con la web
40+
URL url = new URL("https://holamundo.day/");
41+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
42+
connection.setRequestMethod("GET");
43+
connection.connect();
44+
45+
//Recuperamos el código de respuesta
46+
int code = connection.getResponseCode();
47+
System.out.println("Resultado: " + code);
48+
49+
//Leo el contenido de la web y lo guardo en una variable
50+
Scanner scanner = new Scanner(connection.getInputStream());
51+
StringBuilder html = new StringBuilder("");
52+
while (scanner.hasNextLine()) {
53+
html.append(scanner.nextLine());
54+
}
55+
scanner.close();
56+
57+
//Si la petición fue exitosa
58+
if (code == 200) {
59+
//Imprimo por consola el contenido de la web
60+
System.out.println("Contenido de la web: ");
61+
System.out.println(html);
62+
}
63+
64+
} catch (IOException e) {
65+
e.printStackTrace();
66+
}
67+
68+
/*DIFICULTAD EXTRA*/
69+
try {
70+
System.out.println("Poke api: ");
71+
72+
System.out.print("Introduce el nombre o id de un Pokemon: ");
73+
Scanner scanner = new Scanner(System.in);
74+
String nombre = scanner.nextLine();
75+
System.out.println();
76+
77+
//Establecemos la conexón con la web
78+
URL url = new URL("https://pokeapi.co/api/v2/pokemon/" + nombre);
79+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
80+
connection.setRequestMethod("GET");
81+
connection.connect();
82+
int code = connection.getResponseCode();
83+
84+
//Leo el contenido y lo guardo en una variable
85+
scanner = new Scanner(connection.getInputStream());
86+
StringBuilder json = new StringBuilder("");
87+
while (scanner.hasNextLine()) {
88+
json.append(scanner.nextLine());
89+
}
90+
91+
//Si la petición fue exitosa
92+
if (code == 200) {
93+
//Imprimo por consola el contenido
94+
Gson gson = new Gson();
95+
JsonObject data = gson.fromJson(json.toString(), JsonObject.class);
96+
97+
String name = data.get("name").getAsString();
98+
int id = data.get("id").getAsInt();
99+
int weight = data.get("weight").getAsInt();
100+
int height = data.get("height").getAsInt();
101+
102+
ArrayList<String> nameGames = new ArrayList<>();
103+
JsonArray games = data.getAsJsonArray("game_indices");
104+
for (int i = 0; i < games.size(); i++) {
105+
JsonObject game = games.get(i).getAsJsonObject();
106+
String nameGame = game.getAsJsonObject("version").get("name").getAsString();
107+
nameGames.add(nameGame);
108+
}
109+
110+
ArrayList<String> nameTypes = new ArrayList();
111+
JsonArray types = data.getAsJsonArray("types");
112+
for (JsonElement element : types) {
113+
String nameType = element.getAsJsonObject().getAsJsonObject("type").get("name").getAsString();
114+
nameTypes.add(nameType);
115+
}
116+
117+
//Establecemos conexión para recuperar evoluciones
118+
url = new URL("https://pokeapi.co/api/v2/pokemon-species/"+id);
119+
connection = (HttpURLConnection) url.openConnection();
120+
connection.setRequestMethod("GET");
121+
connection.connect();
122+
code = connection.getResponseCode();
123+
124+
//Leo el contenido y lo guardo en una variable
125+
scanner = new Scanner(connection.getInputStream());
126+
StringBuilder jsonSpecies = new StringBuilder("");
127+
while (scanner.hasNextLine()) {
128+
jsonSpecies.append(scanner.nextLine());
129+
}
130+
131+
//Si la petición fue exitosa
132+
if (code == 200) {
133+
JsonObject dataSpecies = gson.fromJson(jsonSpecies.toString(), JsonObject.class);
134+
url = new URL(dataSpecies.getAsJsonObject("evolution_chain").get("url").getAsString());
135+
connection = (HttpURLConnection) url.openConnection();
136+
connection.setRequestMethod("GET");
137+
connection.connect();
138+
code = connection.getResponseCode();
139+
140+
//Leo el contenido y lo guardo en una variable
141+
scanner = new Scanner(connection.getInputStream());
142+
StringBuilder jsonEvolves = new StringBuilder("");
143+
while (scanner.hasNextLine()) {
144+
jsonEvolves.append(scanner.nextLine());
145+
}
146+
147+
//Si la petición fue exitosa
148+
if (code == 200) {
149+
JsonObject dataEvolves = gson.fromJson(jsonEvolves.toString(), JsonObject.class);
150+
getEvolve(dataEvolves.getAsJsonObject("chain"));
151+
}
152+
}
153+
154+
//Mostramos todos los datos del pokemon
155+
System.out.println("Datos de la pokeApi: ");
156+
System.out.println("Name: " + name);
157+
System.out.println("Id: " + id);
158+
System.out.println("Weight: " + weight);
159+
System.out.println("Height: " + height);
160+
System.out.println("Types: " + nameTypes);
161+
System.out.println("Evolutions: " + nameEvolves);
162+
System.out.println("Games: " + nameGames);
163+
}
164+
165+
//Cerramos los recursos
166+
scanner.close();
167+
} catch (IOException e) {
168+
e.printStackTrace();
169+
}
170+
171+
}
172+
}

0 commit comments

Comments
 (0)