【问题标题】:Rxjava2 with Retrofit get single and all data带有 Retrofit 的 Rxjava2 获取单个和所有数据
【发布时间】:2019-05-13 17:34:00
【问题描述】:

我开始学习 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;

}

主类:

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

【问题讨论】:

    标签: android retrofit rx-java2


    【解决方案1】:
    1. baseUrl 应该是https://restcountries.eu/rest/v2/
    2. 您传递给@Path 注释的密钥必须与您在相对网址中的密钥相同。例如

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

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

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

    端点返回一个 json 数组,因此您必须将 Single&lt;Country&gt; 更改为 Single&lt;List&lt;Country&gt;&gt;

    【讨论】:

      猜你喜欢
      • 2018-11-05
      • 2020-03-19
      • 2018-02-27
      • 2015-07-10
      • 2017-08-06
      • 1970-01-01
      • 2018-10-25
      • 2019-08-29
      • 2018-02-09
      相关资源
      最近更新 更多