1
votes

I started to learn some usage with Retrofit and Rxjava2. I am trying to get all countries from https://restcountries.eu/rest/v2/all/ But cant get my head around it. I wish to get all from url above and sigle like so https://restcountries.eu/rest/v2/name/usa. Can you help me achive that.

public interface ApiService {

@GET("country/{country_id}")
Single<Country> getCountryData(@Path("name") String name);

@GET("country/{country_id}")
Call<Country> getAllCountries(@Path("array") String name);
}

Country:

public class Country {

@Expose
@SerializedName("name")
private Integer name;
@Expose
@SerializedName("capital")

private String capital;
@Expose
@SerializedName("population")
private int population;

}

MainClass:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //Get all
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://restcountries.eu/rest/v2/all/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        ApiService apiService = retrofit.create(ApiService.class);
            apiService.getAllCountries("array").subscribe(new SingleObserver<Country>() {
            @Override
            public void onSubscribe(Disposable d) {
                // we'll come back to this in a moment
            }

            @Override
            public void onSuccess(Country country) {
                // data is ready and we can update the UI
                Log.d("DTAG",country.getName());
            }
            @Override
            public void onError(Throwable e) {
                // oops, we best show some error message
            }
        });;

        //Gat Single
        Retrofit retrofitSingle = new Retrofit.Builder()
                .baseUrl("https://restcountries.eu/rest/v2/all/USA")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        ApiService apiServiceSingle = retrofitSingle.create(ApiService.class);
        apiServiceSingle.getAllCountries("array").subscribe(new SingleObserver<Country>() {
            @Override
            public void onSubscribe(Disposable d) {
                // we'll come back to this in a moment
            }

            @Override
            public void onSuccess(Country country) {
                // data is ready and we can update the UI
                Log.d("DTAG","");
            }
            @Override
            public void onError(Throwable e) {
                // oops, we best show some error message
            }
        });;
    }
1

1 Answers

2
votes
  1. baseUrl should be https://restcountries.eu/rest/v2/
  2. The key you pass to the @Path annotation has to be the same that you have in your relative url. Eg

    @GET("name/{country_id}")
    Single<List<Country>> getCountry(@Path("country_id}") String name)
    

in this case, if you call service.getCountry("usa"), the yielded url will be

https://restcountries.eu/rest/v2/name/usa

The endpoint returns a json array, so you will have to change Single<Country> in Single<List<Country>>