我需要实现下面这段代码,但是应该是耗时太长放在主界面会卡,所以希望异步处理
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,并且把线程转到后台,在前台接收到这两个数据就好了
迷茫2017-04-17 17:40:54
帮楼主写了下示例代码,像楼主这种需求,其实无须操作符进行变换的。
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
}
});
伊谢尔伦2017-04-17 17:40:54
你需要的是cityDataList这个list吧?
那么就可以使用Observable.create来新建一个Observable,把所有操作放在OnSubscribe对象中的call方法中,在最后数据准备完成后调用subscriber相应的onNext就行了。同时处理一下对应的onCompleted和onError