我使用RxJava和Okhttp下载文件。例如,我需要用于下载文件的URL和用于创建名称的名称。但我一次只能得到一个参数。
有没有办法同时获取网址和名称?
api.getRatedWallpapers()
.flatMap(Observable::from)
.map(wallpaper -> wallpaper.detail)
.filter(details -> details != null && details.size() != 0)
.flatMap(Observable::from)
.filter(detail1 -> detail1.width > 1920)
.map(detail -> detail.il_file)
.map(s -> new Request.Builder().url(s).build())
.map(client::newCall)
.map(call -> {
try {
return call.execute();
} catch (IOException e) {
e.printStackTrace();
}
return null;
})
.map(response -> {
try {
return response.body().byteStream();
} catch (IOException e) {
e.printStackTrace();
}
return null;
})
.map(BufferedInputStream::new)
.subscribe(bufferedInputStream -> {
//Here I should use the name in the Detail instance.
File file = new File(random.nextInt(10000) + ".jpg");
try {
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[512];
int hasRead;
while ((hasRead = bufferedInputStream.read(buffer)) != -1) {
fos.write(buffer, 0, hasRead);
}
fos.close();
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
},
Throwable::printStackTrace,
() -> System.out.println("Completed"));