Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何停止应用程序,直到可观察到的线程结束?_Java_Android_Observable_Retrofit_Rx Java2 - Fatal编程技术网

Java 如何停止应用程序,直到可观察到的线程结束?

Java 如何停止应用程序,直到可观察到的线程结束?,java,android,observable,retrofit,rx-java2,Java,Android,Observable,Retrofit,Rx Java2,我正在使用改型和RxJava2。在我的应用程序中,我有一个名为apiServiceGroups的可观察的。据我所知,Observable在另一个线程中运行,而应用程序继续在主线程中工作 我希望应用程序的主线程等到这部分代码运行完毕。我怎么做 我的代码: disposable=apiServiceGroups.getGroupsList(String.format(“{\”offset\”:%s}”,groupCount)) .subscribeOn(Schedulers.io()) .obser

我正在使用改型RxJava2。在我的应用程序中,我有一个名为
apiServiceGroups
可观察的
。据我所知,
Observable
在另一个线程中运行,而应用程序继续在主线程中工作

我希望应用程序的主线程等到这部分代码运行完毕。我怎么做

我的代码:

disposable=apiServiceGroups.getGroupsList(String.format(“{\”offset\”:%s}”,groupCount))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.订阅(新消费者){
@凌驾
public void accept(GroupsListResponse GroupsListResponse)引发异常{
对于(int i=0;i
在accept方法中完成API任务之前,必须隐藏所有视图/片段并显示加载指示器或进度条

一旦完成并得到响应,就必须隐藏加载指示器并显示视图/片段

API调用

  public LiveData<Boolean> apiCall() {
    MutableLiveData<Boolean> loading = new MutableLiveData();
    
      Disposable  disposable = apiServiceGroups.getGroupsList(String.format("{\"offset\":%s}", groupCount))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<GroupsListResponse>() {
                    @Override
                    public void accept(GroupsListResponse groupsListResponse) throws Exception {
                        for (int i = 0; i < groupsListResponse.getGroups().size(); i++) {
                            String currentName = groupsListResponse.getGroups().get(i).getGroupFullName();

                            if (groupName.equals(currentName.split(" ")[0])) {
                                familiar.add(currentName);
                            }
                        }
                        loading.postValue(true);
                    }
                , new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.e("thro", Objects.requireNonNull(throwable.getMessage()));
                    }
                    }
                    });
           return loading;   
                    
    }
  • 在活动/片段中实现接口

  • 将其传递给API方法

  • 在成功/失败响应时,使用回调

    @重写公共void accept(GroupsListResponse GroupsListResponse)引发异常{ 对于(int i=0;i

  • 很抱歉,我仍然不知道如何连接进度条和正在运行的进程。我应该如何停止应用程序并启动进度条?
      final Observer<String> nameObserver = new Observer<String>() {
                @Override
                public void onChanged(@Nullable final String newName) {
                    // Update the UI
                }
            };
            apiCall().observe(this, nameObserver);
    
     interface Response {
            
            public void onSuccess();
    
            public void onFailure();
        }
    
                            if (groupName.equals(currentName.split(" ")[0])) {
                                familiar.add(currentName);
                            }
                        }
                        callback.success()
                    }