Android改型并行文件下载问题

Android改型并行文件下载问题,android,retrofit,rx-java,retrofit2,rx-java2,Android,Retrofit,Rx Java,Retrofit2,Rx Java2,我的应用程序中有一个从服务器下载两个zip文件的用例。为此,我一直在使用改型+rxjava(创建了两个独立的改型服务)。对于并行执行,我已经在新线程中订阅了改造服务,然后使用zip操作符将其组合起来。很好用。但后来我在这两个服务中都添加了map操作符来进行解压缩操作,但它没有执行map操作符中编写的代码,控件直接传递给zip操作。我不知道如何处理这个问题,我是反应世界的新手 到目前为止我一直在尝试什么 Observable<Response<ResponseBody>&

我的应用程序中有一个从服务器下载两个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());
Observable dFileObservable=dbDownloadApi.DownloadDealData(WebServiceConstants.ACTION\u DEALER\u数据,
params.getDealerNumber()、params.getUserId()、params.getClientId()、params.getSessionId()).subscribeOn(Schedulers.newThread());
dFileObservable.map(新函数(){
@凌驾
公共字符串apply(响应响应主体)引发异常{
String header=responseBody.headers().get(“内容处置”);
字符串文件名=标题。替换(“附件;文件名=,”);
String downloadFolderPath=fileManager.makeAndGetDownloadFolderPath();
String dealerZipPath=fileManager.makeFolder(下载folderpath,StrConstants.DEALER\u FOLDER\u NAME);
fileManager.writeDownloadedFileToDisk(dealerZipPath,filename,responseBody.body().source());
字符串dealerFilePath=dealerZipPath+File.separator+filename;
unziputability.unzip(dealerFilePath,fileManager.makeAndGetDownloadFolderPath()+File.separator+StrConstants.GENERAL\u FOLDER\u NAME);
返回dealFilePath;
}
});
Observable generalFileObservable=dbDownloadApi.downloadGeneralData(WebServiceConstants.ACTION\u GENERAL\u数据,
params.getDealerNumber()、params.getUserId()、params.getClientId()、params.getSessionId()).subscribeOn(Schedulers.newThread());;
generalFileObservable.map(新函数(){
@凌驾
公共字符串apply(响应响应主体)引发异常{
String header=responseBody.headers().get(“内容处置”);
字符串文件名=标题。替换(“附件;文件名=,”);
String downloadFolderPath=fileManager.makeAndGetDownloadFolderPath();
字符串generalZipPath=fileManager.makeFolder(下载文件夹路径、StrConstants.GENERAL文件夹名称);
fileManager.writeDownloadedFileToDisk(generalZipPath,filename,responseBody.body().source());
字符串generalFilePath=generalZipPath+File.separator+filename;
unziputability.unzip(generalFilePath、fileManager.makeAndGetDownloadFolderPath()+File.separator+StrConstants.GENERAL\u FOLDER\u NAME);
返回一般文件路径;
}
});
Observable zipped=Observable.zip(DealFileObservable、generalFileObservable、new BiFunction(){
@凌驾
公共字符串apply(响应responseBodyResponse,响应responseBodyResponse2)引发异常{
System.out.println(“压缩的yess”);
返回null;
}
}).observeOn(Schedulers.io());
订阅(getObserver());
以及getObserver()函数

private Observer getObserver(){
返回新的观察者(){
@凌驾
认购的公共无效(一次性d){
}
@凌驾
public void onNext(字符串值){
System.out.println(“--------------总时间------------”;
System.out.println(“结果值-->”+值);
}
@凌驾
公共无效申报人(可丢弃的e){
}
@凌驾
未完成的公共空间(){
}
};
}
当代码执行时,控件被转移到zip操作符中的apply()函数,而两个observable中的map操作符都没有执行

还有一个问题

我正在合并/压缩两个观察值,传递给操作员的类型是响应。实际上,我需要下载的文件路径(字符串类型),为此我应该怎么做

**

更新了@Yaroslav Stavnichy所述的解决方案,目前正在运行

**

Observable deObservable=dbDownloadApi.downloaddata(WebServiceConstants.ACTION_数据,
params.getNumber()、params.getId()、params.getCtId()、params.getSessionId())
.flatMap(新函数(){
@凌驾
公共ObserviceSource apply(响应响应库)引发异常{
字符串zipPath=fileManager.processDownloadedFile(StrConstants.FOLDER\u名称,
StrConstants.FILE_NAME,responseBody.body().source();
返回可观察的。just(zipPath);
}
}).map(新函数(){
@凌驾
公共字符串应用(字符串文件路径)引发异常{
字符串unzipDestinationPath=fileManager.makeAndGetDownloadFolderPath()+
File.separator+StrConstants.FOLDER\u NAME;
解压(文件路径,解压路径);
返回unzipDestinationPath;
}
}).subscribeOn(Schedulers.newThread());

您有效地做的是:

Observable a = ...;
Observable b = ...;
a.map(...);
b.map(...);
Observable.zip(a, b).subscribe(f);
map()。它返回新的可观察值,您可以在进一步的计算中使用。在代码中,您忽略了那些返回的对象。您正在压缩原始观测值,而不是映射的观测值,这就是映射器函数不会被调用的原因

我想你想做以下几件事:

Observable a = ... .map(...);
Observable b = ... .map(...);
Observable.zip(a, b).subscribe(f);

你正在有效地做的是:

Observable a = ...;
Observable b = ...;
a.map(...);
b.map(...);
Observable.zip(a, b).subscribe(f);
map()。它返回新的可观察到的,你可以告诉我们
Observable a = ... .map(...);
Observable b = ... .map(...);
Observable.zip(a, b).subscribe(f);