com.squareup.retrofit2:retrofit:2.3.0
マイ手順:Android:Retrofit:複数の連続した非同期リクエスト。 Тoo多くのネストされたアイテム
- スタート非同期リクエスト:getCompaniesList()
- 待ち成功応答
- スタート別の非同期リクエスト:getCatalogsList()
- 待ち成功応答
- 別のコードを実行する
ここに私の活動のスニペット:
RestClient restClient = RestClientFactory.getRestClient();
Call<List<Company>> companyList = restClient.getCompaniesList(filters);
companyList.enqueue(new Callback<List<Company>>() {
@Override
public void onResponse(Call<List<Company>> call, Response<List<Company>> response) {
if (response.isSuccessful()) {
RestClient restClient = RestClientFactory.getRestClient();
Call<List<Catalog>> catalogList = restClient.getCatalogsList(filters);
catalogList.enqueue(new Callback<List<Catalog>>() {
@Override
public void onResponse(Call<List<Catalog>> call, Response<List<Catalog>> response) {
if (response.isSuccessful()) {
// HERE SOME NEED CODE!!!
}
}
@Override
public void onFailure(Call<List<Catalog>> call, Throwable throwable) {
}
});
}
}
@Override
public void onFailure(Call<List<Company>> call, Throwable throwable) {
}
});
私はこの構造が非常にいいとは思いません。 多くのネストされたアイテム。結果コードはより複雑です。
質問:この構造の代替手段はありますか?
のようにコードを分割することができます。 –
私は同じ問題を抱えています。より見栄えの良いコードのための醜い解決策は、成功したときにインターフェイスを通知し、各インターフェイスで作業を行うことです。しかし、@ジョンが述べたように、[RxJava](https://www.captechconsulting.com/blogs/getting-started-with-rxjava-and-android)をチェックするか、[Otto](http://square.github。 io/otto /)library – riadrifai