在 Android studio java 中将 AsyncTask 转换为 RxJava

问题描述 投票:0回答:0

首先,我是 android studio 上 java 的新手,并尝试使用 asyncTask 使用来自 github 的代码。但是 AsyncTask 已被弃用,因此需要根据我的建议将其替换为 rxJava。如果有人帮助将下面的代码转换为 rxJava,我将不胜感激。谢谢。

私有类 DownloadTask 扩展 AsyncTask {

    // Downloading data in non-ui thread
    @Override
    protected String doInBackground(String... url) {

        // For storing data from web service
        String data = "";

        try{
            // Fetching the data from web service
            data = downloadUrl(url[0]);
            Log.d("DownloadTask","DownloadTask : " + data);
        }catch(Exception e){
            Log.d("Background Task",e.toString());
        }
        return data;
    }

    // Executes in UI thread, after the execution of
    // doInBackground()
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        ParserTask parserTask = new ParserTask();

        // Invokes the thread for parsing the JSON data
        parserTask.execute(result);
    }
}

/** A class to parse the Google Directions in JSON format */
private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String,String>>> >{

    // Parsing the data in non-ui thread
    @Override
    protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {

        JSONObject jObject;
        List<List<HashMap<String, String>>> routes = null;

        try{
            jObject = new JSONObject(jsonData[0]);
            DirectionsJSONParser parser = new DirectionsJSONParser();

            // Starts parsing data
            routes = parser.parse(jObject);
        }catch(Exception e){
            e.printStackTrace();
        }
        return routes;
    }
java android android-studio android-asynctask rx-java
© www.soinside.com 2019 - 2024. All rights reserved.