我的应用程序中有一个用例,用于从服务器下载两个zip文件。为此,我一直在使用改造+ rxjava(创建两个单独的改造服务)。对于并行,我执行已经在新线程中订阅了改装服务,然后使用zip运算符进行组合。它工作正常。但后来我将map操作符添加到两个服务中以进行解压缩操作,但它不执行在map操作符中编写的代码,并且控件直接传递给zip操作。我不知道如何解决这个问题,而且我是反应世界的新手。
到目前为止我尝试了什么
Observable<Response<ResponseBody>> dFileObservable = dbDownloadApi.downloadDealerData(WebServiceConstants.ACTION_DEALER_DATA,
params.getDealerNumber(),params.getUserId(),params.getClientId(), params.getSessionId()).subscribeOn(Schedulers.newThread());
dFileObservable.map(new Function<Response<ResponseBody>, String>() {
@Override
public String apply(Response<ResponseBody> responseBody) throws Exception {
String header = responseBody.headers().get("Content-Disposition");
String filename = header.replace("attachment; filename=", "");
String downloadFolderPath = fileManager.makeAndGetDownloadFolderPath();
String dealerZipPath = fileManager.makeFolder(downloadFolderPath, StrConstants.DEALER_FOLDER_NAME);
fileManager.writeDownloadedFileToDisk(dealerZipPath,filename, responseBody.body().source());
String dealerFilePath = dealerZipPath+File.separator+filename;
unzipUtility.unzip(dealerFilePath, fileManager.makeAndGetDownloadFolderPath()+File.separator+ StrConstants.GENERAL_FOLDER_NAME);
return dealerFilePath;
}
});
Observable<Response<ResponseBody>> generalFileObservable = dbDownloadApi.downloadGeneralData(WebServiceConstants.ACTION_GENERAL_DATA,
params.getDealerNumber(),params.getUserId(),params.getClientId(), params.getSessionId()).subscribeOn(Schedulers.newThread());;
generalFileObservable.map(new Function<Response<ResponseBody>, String>() {
@Override
public String apply(Response<ResponseBody> responseBody) throws Exception {
String header = responseBody.headers().get("Content-Disposition");
String filename = header.replace("attachment; filename=", "");
String downloadFolderPath = fileManager.makeAndGetDownloadFolderPath();
String generalZipPath = fileManager.makeFolder(downloadFolderPath, StrConstants.GENERAL_FOLDER_NAME);
fileManager.writeDownloadedFileToDisk(generalZipPath,filename, responseBody.body().source());
String generalFilePath = generalZipPath+File.separator+filename;
unzipUtility.unzip(generalFilePath, fileManager.makeAndGetDownloadFolderPath()+File.separator+ StrConstants.GENERAL_FOLDER_NAME);
return generalFilePath;
}
});
Observable<String> zipped = Observable.zip(dealerFileObservable, generalFileObservable, new BiFunction<Response<ResponseBody>, Response<ResponseBody>, String>() {
@Override
public String apply(Response<ResponseBody> responseBodyResponse, Response<ResponseBody> responseBodyResponse2) throws Exception {
System.out.println("zipped yess");
return null;
}
}).observeOn(Schedulers.io());
zipped.subscribe(getObserver());
和getObserver()函数
private Observer<String> getObserver(){
return new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String value) {
System.out.println("------------total time-----------");
System.out.println("result value-->"+value);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
};
}
当代码执行时,控件被转移到zip运算符中的apply()函数,并且两个observable中的map运算符都没有被执行。
还有另一个问题
我正在合并/压缩两个observable,传递给运算符的类型是 Response&lt;“ResponseBody”&gt; 。实际上我需要下载的文件路径(字符串类型),为此我该怎么办?
**
更新了@Yaroslav Stavnichiy描述的解决方案,现在正在使用
**
Observable<String> deObservable = dbDownloadApi.downloaddData(WebServiceConstants.ACTION_DATA,
params.getNumber(),params.getId(),params.getCtId(), params.getSessionId())
.flatMap(new Function<Response<ResponseBody>, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(Response<ResponseBody> responseBody) throws Exception {
String zipPath = fileManager.processDownloadedFile(StrConstants.FOLDER_NAME,
StrConstants.FILE_NAME,responseBody.body().source());
return Observable.just(zipPath);
}
}).map(new Function<String, String>() {
@Override
public String apply(String filePath) throws Exception {
String unzipDestinationPath = fileManager.makeAndGetDownloadFolderPath()+
File.separator+ StrConstants.FOLDER_NAME;
unzipUtility.unzip(filePath, unzipDestinationPath);
return unzipDestinationPath;
}
}).subscribeOn(Schedulers.newThread());
答案 0 :(得分:2)
你实际做的是:
Observable a = ...;
Observable b = ...;
a.map(...);
b.map(...);
Observable.zip(a, b).subscribe(f);
map()
(以及所有其他rx-operators)不会改变源。它返回新的observable,您可以在进一步的计算中使用它。在您的代码中,您忽略了那些返回的对象。您正在压缩原始的observable,而不是映射的,这就是为什么不调用mapper函数。
我认为您想要执行以下操作:
Observable a = ... .map(...);
Observable b = ... .map(...);
Observable.zip(a, b).subscribe(f);