search

Home  >  Q&A  >  body text

android - 有数据要处理的时候如何使用rxJava进行异步处理数据

我需要实现下面这段代码,但是应该是耗时太长放在主界面会卡,所以希望异步处理

        JsonReader jsonReader = new JsonReader();
        cityObject = jsonReader.jsonObject();
        try {
            cityArray = cityObject.getJSONArray("city_info");
            for (int i=0;i<cityArray.length();i++) {
                Gson gson = new Gson();
                JSONObject city = (JSONObject) cityArray.get(i);
                cityDataList.add(gson.fromJson(city.toString(), CityData.class));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        provinceList = getProvinceList();
        cityMap = getCityMap();
public class JsonReader {

    public JSONObject jsonObject(){
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("assets/cityList.json");
        InputStreamReader streamReader = new InputStreamReader(inputStream);
        BufferedReader reader = new BufferedReader(streamReader);
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
            reader.close();
            reader.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            JSONObject cityJson = new JSONObject(stringBuilder.toString());
            return cityJson;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

}
private List<String> getProvinceList() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < cityDataList.size(); i++) {
            CityData cityData = cityDataList.get(i);
            boolean isProvince = false;
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).equals(cityData.getProv())) {
                    isProvince = true;
                    break;
                }
            }
            if (isProvince) continue;
            else list.add(cityData.getProv());
        }
        return list;
    }


    private HashMap<String, List<String>> getCityMap() {
        HashMap<String, List<String>> map = new HashMap<>();
        for (int i = 0; i < provinceList.size(); i++) {
            String prov = provinceList.get(i);
            List<String> list = new ArrayList<>();
            for (int j = 0; j < cityDataList.size(); j++) {
                if (cityDataList.get(j).getProv().equals(prov))
                    list.add(cityDataList.get(j).getCity());
            }
            map.put(prov, list);
        }
        return map;
    }

这几个操作循环比较多,比较耗时,我想用rxjava处理,但是对observable的使用还是不太清楚,有人能介绍一下像我这样observable怎么写吗?
我想要改进的是用observable通过flatmap等这些操作符来返回provinceList和cityMap,并且把线程转到后台,在前台接收到这两个数据就好了

ringa_leeringa_lee2772 days ago731

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-17 17:40:54

    I wrote the sample code for the original poster. For the requirements like the original poster, there is actually no need for operators to perform transformations.

            Observable.create(new Observable.OnSubscribe<Pair>() {
                @Override
                public void call(Subscriber<? super Pair> subscriber) {
                    JsonReader jsonReader = new JsonReader();
                    cityObject = jsonReader.jsonObject();
                    try {
                        cityArray = cityObject.getJSONArray("city_info");
                        for (int i=0;i<cityArray.length();i++) {
                            Gson gson = new Gson();
                            JSONObject city = (JSONObject) cityArray.get(i);
                            cityDataList.add(gson.fromJson(city.toString(), CityData.class));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    provinceList = getProvinceList();
                    cityMap = getCityMap();
                    Pair pair = new Pair(provinceList, cityMap);
                }
            }).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<Pair>() {
                @Override
                public void onCompleted() {
    
                }
    
                @Override
                public void onError(Throwable e) {
    
                }
    
                @Override
                public void onNext(Pair pair) {
                    //pair.first -> provinceList
                    //pair.second -> cityMap
                }
            });

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:40:54

    What you need is the cityDataList list, right?
    Then you can use Observable.create to create a new Observable, put all operations in the call method in the OnSubscribe object, and call the corresponding onNext of the subscriber after the final data preparation is completed. Handle the corresponding onCompleted and onError at the same time

    reply
    0
  • Cancelreply