Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Rxjava2经过改造,可获取单个和所有数据_Android_Retrofit_Rx Java2 - Fatal编程技术网

Android Rxjava2经过改造,可获取单个和所有数据

Android Rxjava2经过改造,可获取单个和所有数据,android,retrofit,rx-java2,Android,Retrofit,Rx Java2,我开始学习改型和Rxjava2的一些用法。我正试图让所有的国家都来,但我的头脑还不能适应。我希望从上面的url和sigle获得所有信息。你能帮我做到吗 public interface ApiService { @GET("country/{country_id}") Single<Country> getCountryData(@Path("name") String name); @GET("country/{country_id}") Call<Country>

我开始学习改型和Rxjava2的一些用法。我正试图让所有的国家都来,但我的头脑还不能适应。我希望从上面的url和sigle获得所有信息。你能帮我做到吗

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);
}
主要类别:

 @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
            }
        });;
    }
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//全部
改装改装=新改装.Builder()
.baseUrl(“https://restcountries.eu/rest/v2/all/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
ApiService ApiService=reformation.create(ApiService.class);
apiService.getAllCountries(“数组”).subscribe(新的SingleObserver(){
@凌驾
认购的公共无效(一次性d){
//我们一会儿再来讨论这个问题
}
@凌驾
成功时的公共无效(国家/地区){
//数据准备就绪,我们可以更新UI
Log.d(“DTAG”,country.getName());
}
@凌驾
公共无效申报人(可丢弃的e){
//哦,我们最好显示一些错误消息
}
});;
//单曲
改装改装单=新改装.Builder()
.baseUrl(“https://restcountries.eu/rest/v2/all/USA")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
ApiService-apiServiceSingle=reportingsingle.create(ApiService.class);
apiServiceSingle.getAllCountries(“数组”).subscribe(新的SingleObserver()){
@凌驾
认购的公共无效(一次性d){
//我们一会儿再来讨论这个问题
}
@凌驾
成功时的公共无效(国家/地区){
//数据准备就绪,我们可以更新UI
Log.d(“DTAG”和“);
}
@凌驾
公共无效申报人(可丢弃的e){
//哦,我们最好显示一些错误消息
}
});;
}
  • baseUrl应为
    https://restcountries.eu/rest/v2/
  • 传递给
    @Path
    注释的键必须与相对url中的键相同。乙二醇

    @GET("name/{country_id}")
    Single<List<Country>> getCountry(@Path("country_id}") String name)
    
    @GET(“name/{country\u id}”)
    单个getCountry(@Path(“country_id}”)字符串名)
    
  • 在这种情况下,如果调用
    service.getCountry(“usa”)
    ,则生成的url将是

    端点返回一个json数组,因此您必须在
    Single

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