首页 > android > 如何在后台线程中使用RX从android资产文件夹中读取文件

如何在后台线程中使用RX从android资产文件夹中读取文件 (How to read file from asset folder in android using RX in background thread)

2019-03-07 androidrx-java

问题

private ModelObject model;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

    }

    private void readFile() {
        if (model == null) {
            Gson gson = new Gson();
            final String helpItem = “my _file.json”;
            InputStream stream = null;
            try {
                stream = getResources().getAssets().open(helpItem);
                Reader reader = new InputStreamReader(stream);
                model = gson.fromJson(reader, ModelObjects.class);
                reader.close();
                stream.close();
            } catch (IOException e) {
                Timber.w(e);
            } finally {
                fileclose();
            }
        }
    }

这是我使用此代码的代码我正在从资源文件夹中读取文件并解析为Model对象但是我想在后台线程中读取此文件而不是主线程所以获取数据主线程请建议我如何在后台实现读取文件线程并在主线程中获取值。

解决方法

你可以这样做Observable

Observable.fromCallable(new Callable<ModelObject>() {
      @Override public ModelObject call() throws Exception {
        return readFile(); // Make readFile so that it returns ModelObject
      }
    })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<ModelObject>() {
          @Override public void onSubscribe(Disposable d) {

          }

          @Override public void onNext(ModelObject o) {

          }

          @Override public void onError(Throwable e) {

          }

          @Override public void onComplete() {

          }
        });

问题

private ModelObject model;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

    }

    private void readFile() {
        if (model == null) {
            Gson gson = new Gson();
            final String helpItem = “my _file.json”;
            InputStream stream = null;
            try {
                stream = getResources().getAssets().open(helpItem);
                Reader reader = new InputStreamReader(stream);
                model = gson.fromJson(reader, ModelObjects.class);
                reader.close();
                stream.close();
            } catch (IOException e) {
                Timber.w(e);
            } finally {
                fileclose();
            }
        }
    }

This is my code using this code i am reading file from assets folder and parse to Model object but i want to read this file in back ground thread instead of Main thread so and get data main thread please suggest me how to implement read file in background thread and get value in Main thread.

解决方法

You can do this with Observable like this:

Observable.fromCallable(new Callable<ModelObject>() {
      @Override public ModelObject call() throws Exception {
        return readFile(); // Make readFile so that it returns ModelObject
      }
    })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<ModelObject>() {
          @Override public void onSubscribe(Disposable d) {

          }

          @Override public void onNext(ModelObject o) {

          }

          @Override public void onError(Throwable e) {

          }

          @Override public void onComplete() {

          }
        });
相似信息