首页设备列表
This commit is contained in:
@@ -1,171 +1,170 @@
|
||||
package com.ckkj.water.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.ckkj.water.fate.entity.CityDataEntity;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author fengxiaoyang
|
||||
* @date 2025/8/11
|
||||
* @description 城市信息JSON处理工具类
|
||||
*/
|
||||
|
||||
public class YfjJsonHadnlerUtil {
|
||||
|
||||
private static final String TAG = "YfjJsonHadnlerUtil";
|
||||
private static final String CITY_INFO_FILE = "yfj_city_info.json";
|
||||
|
||||
private static JSONObject cityInfoJson = null;
|
||||
|
||||
/**
|
||||
* 初始化城市信息数据
|
||||
* @param context 上下文
|
||||
*/
|
||||
public static void initCityInfo(Context context) {
|
||||
if (cityInfoJson == null) {
|
||||
cityInfoJson = loadCityInfoFromAssets(context);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从assets文件夹加载城市信息JSON文件
|
||||
* @param context 上下文
|
||||
* @return JSONObject 城市信息数据
|
||||
*/
|
||||
private static JSONObject loadCityInfoFromAssets(Context context) {
|
||||
try {
|
||||
InputStream inputStream = context.getAssets().open(CITY_INFO_FILE);
|
||||
int size = inputStream.available();
|
||||
byte[] buffer = new byte[size];
|
||||
inputStream.read(buffer);
|
||||
inputStream.close();
|
||||
|
||||
String jsonString = new String(buffer, StandardCharsets.UTF_8);
|
||||
return new JSONObject(jsonString);
|
||||
} catch (IOException | JSONException e) {
|
||||
Log.e(TAG, "加载城市信息文件失败: " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据城市名称查找匹配的城市信息
|
||||
* @param cityEntity 城市名称,如"济宁市"、"北京市"、"XX地区"
|
||||
* @return 匹配的城市名称,如果找不到则返回空字符串
|
||||
*/
|
||||
public static String findCityByName(CityDataEntity cityEntity) {
|
||||
String cityResult = "";
|
||||
if (cityInfoJson == null) {
|
||||
Log.e(TAG, "城市信息未初始化,请先调用initCityInfo方法");
|
||||
return "";
|
||||
}
|
||||
|
||||
if (cityEntity == null || cityEntity.getCityName().trim().isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
try {
|
||||
// 遍历所有省份/直辖市
|
||||
JSONArray provinceNames = cityInfoJson.names();
|
||||
if (provinceNames != null) {
|
||||
for (int i = 0; i < provinceNames.length(); i++) {
|
||||
String provinceName = provinceNames.getString(i);
|
||||
|
||||
// 检查省份名称是否包含城市名称
|
||||
if (cityEntity.getCityName().contains(provinceName) || provinceName.contains(cityEntity.getCityName())) {
|
||||
String proName = provinceName.replace("市","");
|
||||
return proName;
|
||||
}else if(cityEntity.getCityType().equals("特别行政区")){
|
||||
|
||||
}
|
||||
if(cityEntity.getProvinceName().equals(provinceName)){
|
||||
// 获取该省份下的城市列表
|
||||
JSONArray cityList = cityInfoJson.getJSONArray(provinceName);
|
||||
if (cityList != null) {
|
||||
for (int j = 0; j < cityList.length(); j++) {
|
||||
String city = cityList.getString(j);
|
||||
|
||||
// 检查城市名称是否包含城市名称参数,或者城市名称参数是否包含城市名称
|
||||
if ((cityEntity.getCityName().contains(city) || city.contains(cityEntity.getCityName()))
|
||||
&& cityEntity.getProvinceName().equals(provinceName)) {
|
||||
cityResult = city;
|
||||
return cityResult;
|
||||
}else{
|
||||
cityResult = cityEntity.getCapitalCity();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "解析城市信息失败: " + e.getMessage());
|
||||
}
|
||||
|
||||
return cityResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有省份名称
|
||||
* @return 省份名称列表
|
||||
*/
|
||||
public static List<String> getAllProvinces() {
|
||||
List<String> provinces = new ArrayList<>();
|
||||
|
||||
if (cityInfoJson == null) {
|
||||
return provinces;
|
||||
}
|
||||
|
||||
try {
|
||||
JSONArray provinceNames = cityInfoJson.names();
|
||||
if (provinceNames != null) {
|
||||
for (int i = 0; i < provinceNames.length(); i++) {
|
||||
provinces.add(provinceNames.getString(i));
|
||||
}
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "获取省份列表失败: " + e.getMessage());
|
||||
}
|
||||
|
||||
return provinces;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据省份名称获取该省份下的城市列表
|
||||
* @param provinceName 省份名称
|
||||
* @return 城市列表
|
||||
*/
|
||||
public static List<String> getCitiesByProvince(String provinceName) {
|
||||
List<String> cities = new ArrayList<>();
|
||||
|
||||
if (cityInfoJson == null || provinceName == null || provinceName.trim().isEmpty()) {
|
||||
return cities;
|
||||
}
|
||||
|
||||
try {
|
||||
if (cityInfoJson.has(provinceName)) {
|
||||
JSONArray cityList = cityInfoJson.getJSONArray(provinceName);
|
||||
if (cityList != null) {
|
||||
for (int i = 0; i < cityList.length(); i++) {
|
||||
cities.add(cityList.getString(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "获取城市列表失败: " + e.getMessage());
|
||||
}
|
||||
|
||||
return cities;
|
||||
}
|
||||
}
|
||||
//package com.ckkj.water.utils;
|
||||
//
|
||||
//import android.content.Context;
|
||||
//import android.util.Log;
|
||||
//
|
||||
//
|
||||
//import org.json.JSONArray;
|
||||
//import org.json.JSONException;
|
||||
//import org.json.JSONObject;
|
||||
//
|
||||
//import java.io.IOException;
|
||||
//import java.io.InputStream;
|
||||
//import java.nio.charset.StandardCharsets;
|
||||
//import java.util.ArrayList;
|
||||
//import java.util.List;
|
||||
//
|
||||
///**
|
||||
// * @author fengxiaoyang
|
||||
// * @date 2025/8/11
|
||||
// * @description 城市信息JSON处理工具类
|
||||
// */
|
||||
//
|
||||
//public class YfjJsonHadnlerUtil {
|
||||
//
|
||||
// private static final String TAG = "YfjJsonHadnlerUtil";
|
||||
// private static final String CITY_INFO_FILE = "yfj_city_info.json";
|
||||
//
|
||||
// private static JSONObject cityInfoJson = null;
|
||||
//
|
||||
// /**
|
||||
// * 初始化城市信息数据
|
||||
// * @param context 上下文
|
||||
// */
|
||||
// public static void initCityInfo(Context context) {
|
||||
// if (cityInfoJson == null) {
|
||||
// cityInfoJson = loadCityInfoFromAssets(context);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 从assets文件夹加载城市信息JSON文件
|
||||
// * @param context 上下文
|
||||
// * @return JSONObject 城市信息数据
|
||||
// */
|
||||
// private static JSONObject loadCityInfoFromAssets(Context context) {
|
||||
// try {
|
||||
// InputStream inputStream = context.getAssets().open(CITY_INFO_FILE);
|
||||
// int size = inputStream.available();
|
||||
// byte[] buffer = new byte[size];
|
||||
// inputStream.read(buffer);
|
||||
// inputStream.close();
|
||||
//
|
||||
// String jsonString = new String(buffer, StandardCharsets.UTF_8);
|
||||
// return new JSONObject(jsonString);
|
||||
// } catch (IOException | JSONException e) {
|
||||
// Log.e(TAG, "加载城市信息文件失败: " + e.getMessage());
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 根据城市名称查找匹配的城市信息
|
||||
// * @param cityEntity 城市名称,如"济宁市"、"北京市"、"XX地区"
|
||||
// * @return 匹配的城市名称,如果找不到则返回空字符串
|
||||
// */
|
||||
// public static String findCityByName(CityDataEntity cityEntity) {
|
||||
// String cityResult = "";
|
||||
// if (cityInfoJson == null) {
|
||||
// Log.e(TAG, "城市信息未初始化,请先调用initCityInfo方法");
|
||||
// return "";
|
||||
// }
|
||||
//
|
||||
// if (cityEntity == null || cityEntity.getCityName().trim().isEmpty()) {
|
||||
// return "";
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// // 遍历所有省份/直辖市
|
||||
// JSONArray provinceNames = cityInfoJson.names();
|
||||
// if (provinceNames != null) {
|
||||
// for (int i = 0; i < provinceNames.length(); i++) {
|
||||
// String provinceName = provinceNames.getString(i);
|
||||
//
|
||||
// // 检查省份名称是否包含城市名称
|
||||
// if (cityEntity.getCityName().contains(provinceName) || provinceName.contains(cityEntity.getCityName())) {
|
||||
// String proName = provinceName.replace("市","");
|
||||
// return proName;
|
||||
// }else if(cityEntity.getCityType().equals("特别行政区")){
|
||||
//
|
||||
// }
|
||||
// if(cityEntity.getProvinceName().equals(provinceName)){
|
||||
// // 获取该省份下的城市列表
|
||||
// JSONArray cityList = cityInfoJson.getJSONArray(provinceName);
|
||||
// if (cityList != null) {
|
||||
// for (int j = 0; j < cityList.length(); j++) {
|
||||
// String city = cityList.getString(j);
|
||||
//
|
||||
// // 检查城市名称是否包含城市名称参数,或者城市名称参数是否包含城市名称
|
||||
// if ((cityEntity.getCityName().contains(city) || city.contains(cityEntity.getCityName()))
|
||||
// && cityEntity.getProvinceName().equals(provinceName)) {
|
||||
// cityResult = city;
|
||||
// return cityResult;
|
||||
// }else{
|
||||
// cityResult = cityEntity.getCapitalCity();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } catch (JSONException e) {
|
||||
// Log.e(TAG, "解析城市信息失败: " + e.getMessage());
|
||||
// }
|
||||
//
|
||||
// return cityResult;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取所有省份名称
|
||||
// * @return 省份名称列表
|
||||
// */
|
||||
// public static List<String> getAllProvinces() {
|
||||
// List<String> provinces = new ArrayList<>();
|
||||
//
|
||||
// if (cityInfoJson == null) {
|
||||
// return provinces;
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// JSONArray provinceNames = cityInfoJson.names();
|
||||
// if (provinceNames != null) {
|
||||
// for (int i = 0; i < provinceNames.length(); i++) {
|
||||
// provinces.add(provinceNames.getString(i));
|
||||
// }
|
||||
// }
|
||||
// } catch (JSONException e) {
|
||||
// Log.e(TAG, "获取省份列表失败: " + e.getMessage());
|
||||
// }
|
||||
//
|
||||
// return provinces;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 根据省份名称获取该省份下的城市列表
|
||||
// * @param provinceName 省份名称
|
||||
// * @return 城市列表
|
||||
// */
|
||||
// public static List<String> getCitiesByProvince(String provinceName) {
|
||||
// List<String> cities = new ArrayList<>();
|
||||
//
|
||||
// if (cityInfoJson == null || provinceName == null || provinceName.trim().isEmpty()) {
|
||||
// return cities;
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// if (cityInfoJson.has(provinceName)) {
|
||||
// JSONArray cityList = cityInfoJson.getJSONArray(provinceName);
|
||||
// if (cityList != null) {
|
||||
// for (int i = 0; i < cityList.length(); i++) {
|
||||
// cities.add(cityList.getString(i));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } catch (JSONException e) {
|
||||
// Log.e(TAG, "获取城市列表失败: " + e.getMessage());
|
||||
// }
|
||||
//
|
||||
// return cities;
|
||||
// }
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user