带有Retrofit的Rxjava2获得单个数据和所有数据

时间:2018-12-12 15:09:42

标签: android retrofit rx-java2

我开始学习Retrofit和Rxjava2的用法。我正试图从https://restcountries.eu/rest/v2/all/中获得所有国家的支持,但我无法摆脱困境。我希望从上面的url中获取所有信息,并像https://restcountries.eu/rest/v2/name/usa那样单调。你能帮我实现吗?

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);
}

国家/地区:

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 个答案:

答案 0 :(得分:2)

  1. baseUrl应该为https://restcountries.eu/rest/v2/
  2. 您传递给@Path批注的密钥必须与相对URL中的密钥相同。例如

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

在这种情况下,如果您调用service.getCountry("usa"),则生成的网址将是

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

端点返回一个json数组,因此您必须在Single<Country>中更改Single<List<Country>>