开发需要一次性向同一端点发送多个 API 调用的应用程序。
例如 - 目录浏览场景,需要通过对当前文件夹中的所有文件夹发送 get 调用来获取目录结构。问题是,正确改造中的所有文件夹的响应都是分开的,但是 LiveData observable 只为整个列表提供了一个响应。
目录结构:-
test -> temp -> temp1 -> temp2
-> temp3
-> temp4
可观察到以侦听回调:-
mViewModel.getServerFilesLiveData().observe(this, browseServerDataResource -> {
if (browseServerDataResource != null) {
if (browseServerDataResource.status == APIClientStatus.Status.SUCCESS) {
if (browseServerDataResource.data != null) {
Timber.i("Got data for path %s in Observable", browseServerDataResource.data.path);
if (browseServerDataResource.data.folderList != null
&& browseServerDataResource.data.folderList.size() > 0) {
for (final String name : browseServerDataResource.data.folderList) {
final ServerDirectoryPathInfo pathInfo = new ServerDirectoryPathInfo();
pathInfo.completePath = browseServerDataResource.data.path + "/" + name;
getFolderDownloadPath(pathInfo.completePath);
}
}
mFolderCountToParse--;
Timber.d("Folders left to parse %d", mFolderCountToParse);
if (mFolderCountToParse == 0) {
showToast("Parsed all folders");
}
}
}
}
});
调用以获取数据的功能:-
private void getFolderDownloadPath(@NonNull final String path) {
mViewModel.getServerFiles(path);
mFolderCountToParse++;
}
改造对服务器的调用:-
public LiveData<Resource<BrowseServerData>> getServerFiles(@NonNull final String additionalUrl) {
final MutableLiveData<Resource<BrowseServerData>> data = new MutableLiveData<>();
final String url = mMySharedPreferences.getCurrentUrl()
+ AppConstants.DIRECTORY_END_POINT
+ AppConstants.PATH_END_POINT
+ (TextUtils.isEmpty(additionalUrl) ? "" : additionalUrl);
Timber.i("Requesting data for - api %s", url);
mAPI.getServerFiles(url, mMySharedPreferences.getNetworkName())
.enqueue(new Callback<BrowseServerData>() {
@Override
public void onResponse(
@NonNull Call<BrowseServerData> call, @NonNull Response<BrowseServerData> response
) {
if (response.body() != null && response.isSuccessful()) {
if (!TextUtils.isEmpty(response.body().path)) {
Timber.i("Got response for = %s in Retrofit", response.body().path);
}
data.setValue(
new Resource<>(APIClientStatus.Status.SUCCESS, response.body(), null, null));
} else {
ErrorMessage errorMessage = null;
try {
errorMessage = Utility.getApiError(response, mRetrofit);
} catch (IOException e) {
e.printStackTrace();
}
if (errorMessage != null) {
data.setValue(
new Resource<>(APIClientStatus.Status.ERROR, null, errorMessage.message(), call));
} else {
data.setValue(
new Resource<>(APIClientStatus.Status.ERROR, null, response.message(), call));
}
}
}
@Override
public void onFailure(@NonNull Call<BrowseServerData> call, @NonNull Throwable throwable) {
data.setValue(
new Resource<>(APIClientStatus.Status.ERROR, null, throwable.getMessage(), throwable,
call));
}
});
return data;
}
数据如下: -
I: Got response for = ./test in Retrofit
I: Got data for path ./test in Observable
I: Got response for = ./test/temp in Retrofit
I: Got data for path ./test/temp in Observable
I: Got response for = ./test/temp/temp1 in Retrofit
I: Got data for path ./test/temp/temp1 in Observable
I: Got response for = ./test/temp/temp1/temp2 in Retrofit
I: Got response for = ./test/temp/temp1/temp4 in Retrofit
I: Got response for = ./test/temp/temp1/temp3 in Retrofit
I: Got data for path ./test/temp/temp1/temp3 in Observable
如您所见,数据仅针对一个文件夹出现在 Observable 中temp3
。
当在拨打电话时添加随机延迟时,数据会正确出现:-
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
getFolderDownloadPath(pathInfo.completePath);
}
}, new Random().nextInt(10000 - 1000) + 1000);
现在至少数据来自 3 个文件夹中的 2 个:-
I: Got response for = . in Retrofit
I: Got data for path . in Observable
I: Got data for the current directory, don't need it, skipping
I: Got response for = ./test in Retrofit
I: Got data for path ./test in Observable
I: Got response for = ./test/temp in Retrofit
I: Got data for path ./test/temp in Observable
I: Got response for = ./test/temp/temp1 in Retrofit
I: Got data for path ./test/temp/temp1 in Observable
I: Got response for = ./test/temp/temp1/temp3 in Retrofit
I: Got response for = ./test/temp/temp1/temp2 in Retrofit
I: Got data for path ./test/temp/temp1/temp2 in Observable
I: Got response for = ./test/temp/temp1/temp4 in Retrofit
I: Got data for path ./test/temp/temp1/temp4 in Observable
任何想法为什么会发生这种情况以及解决方法?
更新:- 添加有助于调用服务器的 ViewModel 构造函数
@Inject
BrowseHubMediaViewModel(@NonNull Application application, @NonNull APIClient mAPIClient) {
super(application);
mGetServerFilesMutable = new MutableLiveData<>();
mGetServerFilesLiveData =
Transformations.switchMap(mGetServerFilesMutable, mAPIClient::getServerFiles);
}
从 ViewModel 获取 Observable
/**
* Observer to listen for file listing in server
*
* @return LiveData<Resource<BrowseServerData>>
*/
public LiveData<Resource<BrowseServerData>> getServerFilesLiveData() {
return mGetServerFilesLiveData;
}