Error handling 在RxJava出现错误时执行操作
我需要创建一个不存在的文件夹。在我的例子中,唯一的方法是捕获错误并处理它以创建所需的文件夹。 但我能找到的只是Error handling 在RxJava出现错误时执行操作,error-handling,rx-java,rx-android,Error Handling,Rx Java,Rx Android,我需要创建一个不存在的文件夹。在我的例子中,唯一的方法是捕获错误并处理它以创建所需的文件夹。 但我能找到的只是 public static Observable<Boolean> folderExists(final Context context, final String targetPath, final String currentpath) { Application application = Application.get(context); //i brows
public static Observable<Boolean> folderExists(final Context context, final String targetPath, final String currentpath) {
Application application = Application.get(context);
//i browse the folder to get all the items
return browseFolderObservable(context,currentpath)
.subscribeOn(application.defaultSubscribeScheduler())
.doOnError(new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
BsSdkLog.d("Error no file found");
}
})
.map(new Func1<ArrayList<Item>, Boolean>() {
@Override
public Boolean call(ArrayList<Item> items) {
if(items.isEmpty()) {
BsSdkLog.d(" No items");
return false;
}else {
for(int i=0;i<items.size();i++)
{
Item item=items.get(i);
BsSdkLog.d(item.toString());
}
BsSdkLog.d("Right-here");
return true;
}
}
});
}
publicstaticobserveablefolderexists(最终上下文、最终字符串targetPath、最终字符串currentpath){
Application=Application.get(上下文);
//我浏览文件夹以获取所有项目
返回browseFolderObservable(上下文、当前路径)
.subscribeOn(application.defaultSubscribeScheduler())
.doon错误(新操作1(){
@凌驾
公共无效呼叫(可丢弃可丢弃){
BsSdkLog.d(“未找到文件错误”);
}
})
.map(新函数1(){
@凌驾
公共布尔调用(ArrayList项){
if(items.isEmpty()){
BsSdkLog.d(“无项目”);
返回false;
}否则{
对于(inti=0;i基本原理如下所示。我使用JavaNIO库进行测试
方法“createFolder”只是包装创建文件夹。测试“name”调用单一值并检查异常。如果是IOException,它将返回一个回退值。您可以在其中执行其他操作。您只需提供一个回退单一值。如果是与IOException不同的错误,它将返回错误
@Test
void name() throws Exception {
final String TO_CREATE = "/home/sergej/Downloads/Wurstbrot";
this.createFolder(TO_CREATE)
.onErrorResumeNext(throwable -> { // handle Exception:
// Validate Exception
if (throwable instanceof IOException) {
// Return fallback
return Single.just(Paths.get("/home/sergej/Downloads/"));
}
return Single.error(throwable);
})
.test()
.await()
.assertValueCount(1)
.assertValue(path -> path.endsWith(TO_CREATE))
.assertNoErrors();
}
private Single<Path> createFolder(String p) {
return Single.defer(() -> { // may throw some IOException
Path path = Paths.get(p);
if (!Files.exists(path)) {
Path createdDirectory = Files.createDirectory(path); // will throw if already exists
return Single.just(createdDirectory);
}
// Or just return Path, because it already exists???
return Single.error(new IOException("Already exists"));
});
}
@测试
void name()引发异常{
创建的最后一个字符串=“/home/sergej/Downloads/Wurstbrot”;
此.createFolder(要创建)
.onErrorResumeNext(可丢弃->{//句柄异常:
//验证异常
if(IOException的可丢弃实例){
//返回回退
返回Single.just(path.get(“/home/sergej/Downloads/”);
}
返回单个错误(可丢弃);
})
.test()
.等待
.assertValueCount(1)
.assertValue(路径->路径.endsWith(要创建))
.assertNoErrors();
}
私有单个createFolder(字符串p){
返回Single.defer(()->{//可能引发一些IOException
Path=Path.get(p);
如果(!Files.exists(path)){
Path createdDirectory=Files.createDirectory(Path);//如果已经存在,将抛出
返回Single.just(createdDirectory);
}
//或者只是返回路径,因为它已经存在???
返回Single.error(新IOException(“已存在”);
});
}
您想在android设备上写入外部存储器还是内部存储器?