Skip to content

Commit bcf1d77

Browse files
committed
Nuevos ejemplos de RetroFix y RetroFix con RxJava
1 parent 908af2a commit bcf1d77

File tree

12 files changed

+326
-0
lines changed

12 files changed

+326
-0
lines changed

EjemploRetrofit/pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.sanvalero.programacionreactiva</groupId>
8+
<artifactId>ProgramacionReactiva</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>io.reactivex</groupId>
19+
<artifactId>rxjava</artifactId>
20+
<version>1.3.8</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.squareup.retrofit2</groupId>
24+
<artifactId>retrofit</artifactId>
25+
<!-- Para evitar el warning que da la 2.9.0 se puede usar la 2.7.0 -->
26+
<version>2.7.0</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.squareup.retrofit2</groupId>
30+
<artifactId>converter-gson</artifactId>
31+
<version>2.3.0</version>
32+
</dependency>
33+
</dependencies>
34+
35+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.sanvalero.ejemploretrofit;
2+
3+
import com.sanvalero.ejemploretrofit.domain.Country;
4+
import com.sanvalero.ejemploretrofit.service.CountriesService;
5+
6+
import java.util.List;
7+
8+
/**
9+
* Clase principal
10+
* @author Santiago Faci
11+
* @version curso 2020-2021
12+
*/
13+
public class Application {
14+
public static void main(String[] args) {
15+
CountriesService countriesService = new CountriesService();
16+
List<Country> countries = countriesService.getAllCountries();
17+
18+
System.out.println(countries.toString());
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.sanvalero.ejemploretrofit.domain;
2+
3+
/**
4+
* Modelo de objeto que devuelve la API (https://restcountries.eu/)
5+
* @author Santiago Faci
6+
* @version curso 2020-2021
7+
*/
8+
public class Country {
9+
10+
private String name;
11+
private String capital;
12+
private String region;
13+
private String subregion;
14+
15+
@Override
16+
public String toString() {
17+
return name + " [" + capital + "]";
18+
}
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.sanvalero.ejemploretrofit.service;
2+
3+
import com.sanvalero.ejemploretrofit.domain.Country;
4+
import retrofit2.Call;
5+
import retrofit2.http.GET;
6+
import retrofit2.http.Path;
7+
8+
import java.util.List;
9+
10+
/**
11+
* Interfaz de la API (https://restcountries.eu/)
12+
* @author Santiago Faci
13+
* @version curso 2020-2021
14+
*/
15+
public interface CountriesApiService {
16+
17+
@GET("/rest/v2/all")
18+
Call<List<Country>> getAllCountries();
19+
20+
// Cuidado! Aunque la respuesta en un solo país viene en forma de array
21+
@GET("/rest/v2/name/{name}")
22+
Call<List<Country>> getCountry(@Path("name") String name);
23+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.sanvalero.ejemploretrofit.service;
2+
3+
import com.sanvalero.ejemploretrofit.domain.Country;
4+
import retrofit2.Call;
5+
import retrofit2.Response;
6+
import retrofit2.Retrofit;
7+
import retrofit2.converter.gson.GsonConverterFactory;
8+
9+
import java.io.IOException;
10+
import java.util.List;
11+
12+
import static com.sanvalero.ejemploretrofit.util.Constants.URL;
13+
14+
/**
15+
* Service con las operaciones disponibles
16+
* @author Santiago Faci
17+
* @version curso 2020-2021
18+
*/
19+
public class CountriesService {
20+
21+
private CountriesApiService api;
22+
23+
public CountriesService() {
24+
Retrofit retrofit = new Retrofit.Builder()
25+
.baseUrl(URL)
26+
.addConverterFactory(GsonConverterFactory.create())
27+
.build();
28+
29+
api = retrofit.create(CountriesApiService.class);
30+
}
31+
32+
public List<Country> getAllCountries() {
33+
Call<List<Country>> allCountriesCall = api.getAllCountries();
34+
try {
35+
Response<List<Country>> response = allCountriesCall.execute();
36+
return response.body();
37+
} catch (IOException ioe) {
38+
ioe.printStackTrace();
39+
}
40+
41+
return null;
42+
}
43+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.sanvalero.ejemploretrofit.util;
2+
3+
/**
4+
* Constantes
5+
* @author Santiago Faci
6+
* @version curso 2020-2021
7+
*/
8+
public class Constants {
9+
10+
public final static String URL = "https://restcountries.eu/";
11+
12+
}

EjemploRetrofitRxJava/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.sanvalero.programacionreactiva</groupId>
8+
<artifactId>ProgramacionReactiva</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>io.reactivex</groupId>
19+
<artifactId>rxjava</artifactId>
20+
<version>1.3.8</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.squareup.retrofit2</groupId>
24+
<artifactId>retrofit</artifactId>
25+
<version>2.7.0</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.squareup.retrofit2</groupId>
29+
<artifactId>converter-gson</artifactId>
30+
<version>2.3.0</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>com.squareup.retrofit2</groupId>
34+
<artifactId>adapter-rxjava</artifactId>
35+
<version>2.3.0</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.projectlombok</groupId>
39+
<artifactId>lombok</artifactId>
40+
<version>1.18.18</version>
41+
<scope>provided</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.sanvalero.ejemploretrofit;
2+
3+
import com.sanvalero.ejemploretrofit.service.CountriesService;
4+
import rx.Observable;
5+
import rx.schedulers.Schedulers;
6+
7+
import java.util.concurrent.Executors;
8+
9+
/**
10+
* Clase principal
11+
* @author Santiago Faci
12+
* @version curso 2020-2021
13+
*/
14+
public class Application {
15+
public static void main(String[] args) {
16+
final CountriesService countriesService = new CountriesService();
17+
18+
System.out.println("Comenzando descarga . . .");
19+
20+
countriesService.getAllCountries()
21+
.flatMap(Observable::from)
22+
.doOnCompleted(() -> System.out.println("Listado de países descargado"))
23+
.doOnError(throwable -> System.out.println(throwable.getMessage()))
24+
.subscribeOn(Schedulers.from(Executors.newCachedThreadPool()))
25+
.subscribe(System.out::println);
26+
27+
countriesService.getCountry("spain")
28+
.doOnCompleted(() -> System.out.println("Cargada información de spain"))
29+
.subscribeOn(Schedulers.from(Executors.newSingleThreadExecutor()))
30+
.subscribe(System.out::println);
31+
32+
System.out.println("Fin?");
33+
}
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.sanvalero.ejemploretrofit.domain;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* Country
7+
* @author Santiago Faci
8+
* @version Curso 2020-2021
9+
*/
10+
@Data
11+
public class Country {
12+
13+
private String name;
14+
private String capital;
15+
private String region;
16+
private String subregion;
17+
18+
@Override
19+
public String toString() {
20+
return name + " [" + capital + "]";
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.sanvalero.ejemploretrofit.service;
2+
3+
import com.sanvalero.ejemploretrofit.domain.Country;
4+
import retrofit2.http.GET;
5+
import retrofit2.http.Path;
6+
import rx.Observable;
7+
8+
import java.util.List;
9+
10+
/**
11+
* Interfaz de la API para emplear con RetroFit (https://restcountries.eu)
12+
* @author Santiago Faci
13+
* @version Curso 2020-2021
14+
*/
15+
public interface CountriesApiService {
16+
17+
@GET("/rest/v2/all")
18+
Observable<List<Country>> getAllCountries();
19+
20+
// Cuidado! Aunque la respuesta en un solo país viene en forma de array
21+
@GET("/rest/v2/name/{name}")
22+
Observable<List<Country>> getCountry(@Path("name") String name);
23+
}

0 commit comments

Comments
 (0)