将AsyncTask <Void,Void,Void>转换为RxJava

时间:2019-06-18 15:22:52

标签: java android android-asynctask rx-java

我有使用AsyncTask<Void, Void, Void>的这段代码,我想将其转换为RxJava,主要是因为我对此有更多的经验,并且我一生中从未使用过AsyncTask。有人可以帮忙吗?根据已读内容添加尝试,但感觉不对。

原始代码

class TcpConnection extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... voids) {
        socket = null;
        SocketAddress address = new InetSocketAddress(getString(R.string.tcp_server), server_port);
        socket = new Socket();
        try {
            socket.connect(address, 3000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            out = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        output = new PrintWriter(out);
        return null;

    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

                while (true) {
                    String str = "waiting";
                    BufferedReader br = null;
                    try {
                        br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        String line;
                        str = "";

                        while ((line = br.readLine()) != null) {
                            Log.d("read line", line);
                            str = str + line;
                            break;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    final String kk = str;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            if (kk.contains("ACT:")) {
                                Toast.makeText(MainActivity.this, " " + kk, Toast.LENGTH_SHORT).show();
                                ReportHelper.getInstance().execute();

                            }
                        }
                    });

                }
            }

}

我尝试将其转换为RxJava

private void createTCP() {

    Disposable disposable = Observable.fromCallable((Callable<Object>) () -> {
        socket = new Socket();
        SocketAddress address = new InetSocketAddress(MainActivity.this.getString(R.string.tcp_server), server_port);
        try {
            socket.connect(address);
        } catch (IOException e) {
            e.printStackTrace();
            LogUtils.debug(e.getLocalizedMessage());
        }

        try {
            out = socket.getOutputStream();
            output = new PrintWriter(out);
            return output;
        } catch (IOException e) {
            e.printStackTrace();
            LogUtils.debug(e.getLocalizedMessage());
            return null;
        }
    })
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(this::onPostExecute);

    compositeDisposable.add(disposable);

}

private void onPostExecute(Object o) {
    while (true) {
        String str = "waiting";
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            String line;
            str = "";

            while ((line = br.readLine()) != null) {
                Log.d("read line", line);
                str = str + line;
                break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        final String kk = str;

        if (kk.contains("ACT:")) {
            Toast.makeText(MainActivity.this, " " + kk, Toast.LENGTH_SHORT).show();
            ReportHelper.getInstance().execute();
            compositeDisposable.dispose();
        }
    }
}

非常感谢!

0 个答案:

没有答案