package com.jiuqi.njt.register.city;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.jiuqi.njt.R;
import com.jiuqi.njt.data.OptsharepreInterface;
import com.jiuqi.njt.util.Constants;
import com.jiuqi.njt.util.DateUtil;
/**
* 解析本地天气行政区划数据帮助类
* 负责本地mydatastring.txt文件的解析和存储
* @author joe
*
*/
public class AllProvince {
private String TAG = "----------------" + this.getClass().getName();
/**
* 解析mydatastring文件,并将数据存到数据库中
* @param context
*/
public void getProvinceData(Context context){
OptsharepreInterface sharePre = new OptsharepreInterface(context);
CityDataUtil dbHelper = new CityDataUtil(context);
/**
* 通过context对象获取到项目raw文件夹下面的文件流
*/
InputStream input = context.getResources().openRawResource(R.raw.mydatastring);
BufferedReader reader = null;
Reader in = null;
try {
in = new InputStreamReader(input);
reader = new BufferedReader(in);
String tempString = null;
StringBuffer sb = new StringBuffer();
/**
* 把文件中的数据读出来存到StringBuffer中
*/
while ((tempString = reader.readLine()) != null) {
sb.append(tempString);
}
String data = sb.toString();
/**
* 将数据转成字符串后进行拆分处理(通过特殊字符@将所有数据拆成一条条的省市县数据
* --格式为:2044&中国&安徽&宣城&广德&CHINA)
*/
String[] province = data.split("@");
List<CityBean> dataList = new ArrayList<CityBean>();
for (String string : province) {
/**
* 将每条数据拆分成cityBean对象的属性
*/
String[] item = string.split("&");
CityBean cityBean = new CityBean();
for (int i = 0; i < item.length; i++) {
switch (i) {
case 0:
cityBean.setCityId(item[i]);
break;
case 1:
cityBean.setCountryName(item[i]);
break;
case 2:
cityBean.setProvinceName(item[i]);
break;
case 3:
cityBean.setCityName(item[i]);
break;
case 4:
cityBean.setCountyName(item[i]);
break;
case 5:
cityBean.setCountryNameInEnglish(item[i]);
break;
default:
break;
}
}
dataList.add(cityBean);
}
/**
* 通过数据库帮助类把数据存到数据库中
*/
dbHelper.addCityData(dataList);
/**
* 存入标示,标示数据库中已经有了天气预报数据(防止重复添加)
*/
sharePre.putPres(Constants.SAVECITYDATA, 1 + "");
/**
* 发送自定义的广播,通知天气预报省市县数据已经存储完毕
*/
Intent intent = new Intent();
intent.setAction(Constants.SAVECITYDATA);
context.sendBroadcast(intent);
reader.close();
in.close();
input.close();
// for (CityBean cityBean : dataList) {
// Log.e("String解析数据", cityBean.toString());
// }
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取中国所有的省
*
* @return
*/
public List<CityBean> getAllProvinceNew() {
List<CityBean> allProvinceList = new ArrayList<CityBean>();
allProvinceList.add(new CityBean("安徽", 0));
allProvinceList.add(new CityBean("福建", 1));
allProvinceList.add(new CityBean("甘肃", 2));
allProvinceList.add(new CityBean("广东", 3));
// allProvinceList.add(new CityBean("广西", 4)); //自治区
allProvinceList.add(new CityBean("贵州", 5));
allProvinceList.add(new CityBean("海南", 6));
allProvinceList.add(new CityBean("河北", 7));
allProvinceList.add(new CityBean("河南", 8));
allProvinceList.add(new CityBean("黑龙江", 9));
allProvinceList.add(new CityBean("湖北", 10));
allProvinceList.add(new CityBean("湖南", 11));
allProvinceList.add(new CityBean("吉林", 12));
allProvinceList.add(new CityBean("江苏", 13));
allProvinceList.add(new CityBean("江西", 14));
allProvinceList.add(new CityBean("辽宁", 15));
// allProvinceList.add(new CityBean("内蒙古", 16)); //自治区
// allProvinceList.add(new CityBean("宁夏", 17)); //自治区
allProvinceList.add(new CityBean("青海", 18));
allProvinceList.add(new CityBean("山东", 19));
allProvinceList.add(new CityBean("山西", 20));
allProvinceList.add(new CityBean("陕西", 21));
allProvinceList.add(new CityBean("四川", 22));
allProvinceList.add(new CityBean("台湾", 23));
// allProvinceList.add(new CityBean("西藏", 25)); //自治区
// allProvinceList.add(new CityBean("新疆", 26)); //自治区
allProvinceList.add(new CityBean("云南", 27));
allProvinceList.add(new CityBean("浙江", 28));
return allProvinceList;
}
/**
* 获取中国所有的自治区
*
* @return
*/
public List<CityBean> getAllAutonomousRegionNew() {
List<CityBean> allProvinceList = new ArrayList<CityBean>();
allProvinceList.add(new CityBean("广西", 4)); // 自治区
allProvinceList.add(new CityBean("内蒙古", 16)); // 自治区
allProvinceList.add(new CityBean("宁夏", 17)); // 自治区
allProvinceList.add(new CityBean("西藏", 25)); // 自治区
allProvinceList.add(new CityBean("新疆", 26)); // 自治区
return allProvinceList;
}
/**
* 查询中国的所有直辖市
*
* @return
*/
public List<CityBean> getAllMunicipalitiesCityNew() {
List<CityBean> allProvinceList = new ArrayList<CityBean>();
allProvinceList.add(new CityBean("北京", 29)); // 直辖市
allProvinceList.add(new CityBean("上海", 29)); // 直辖市
allProvinceList.add(new CityBean("天津", 29)); // 直辖市
allProvinceList.add(new CityBean("重庆", 29)); // 直辖市
return allProvinceList;
}
/**
* 查询中国的特别行政区
*
* @return
*/
public List<CityBean> getAllSpecialAdministrativeRegionCity() {
List<CityBean> allProvinceList = new ArrayList<CityBean>();
allProvinceList.add(new CityBean("香港", 24)); // 直辖市
allProvinceList.add(new CityBean("澳门", 24)); // 直辖市
return allProvinceList;
}
public List<CityBean> getAllData() {
List<CityBean> allProvinceList = new ArrayList<CityBean>();
allProvinceList.add(new CityBean("安徽", 0));
allProvinceList.add(new CityBean("福建", 1));
allProvinceList.add(new CityBean("甘肃", 2));
allProvinceList.add(new CityBean("广东", 3));
allProvinceList.add(new CityBean("广西", 4)); // 自治区
allProvinceList.add(new CityBean("贵州", 5));
allProvinceList.add(new CityBean("海南", 6));
allProvinceList.add(new CityBean("河北", 7));
allProvinceList.add(new CityBean("河南", 8));
allProvinceList.add(new CityBean("黑龙江", 9));
allProvinceList.add(new CityBean("湖北", 10));
allProvinceList.add(new CityBean("湖南", 11));
allProvinceList.add(new CityBean("吉林", 12));
allProvinceList.add(new CityBean("江苏", 13));
allProvinceList.add(new CityBean("江西", 14));
allProvinceList.add(new CityBean("辽宁", 15));
allProvinceList.add(new CityBean("内蒙古", 16)); // 自治区
allProvinceList.add(new CityBean("宁夏", 17)); // 自治区
allProvinceList.add(new CityBean("青海", 18));
allProvinceList.add(new CityBean("山东", 19));
allProvinceList.add(new CityBean("山西", 20));
allProvinceList.add(new CityBean("陕西", 21));
allProvinceList.add(new CityBean("四川", 22));
allProvinceList.add(new CityBean("台湾", 23));
allProvinceList.add(new CityBean("西藏", 25)); // 自治区
allProvinceList.add(new CityBean("新疆", 26)); // 自治区
allProvinceList.add(new CityBean("云南", 27));
allProvinceList.add(new CityBean("浙江", 28));
allProvinceList.add(new CityBean("香港", 24)); // 特别行政区
// allProvinceList.add(new CityBean("澳门", 24)); //特别行政区
allProvinceList.add(new CityBean("北京", 29)); // 直辖市
// allProvinceList.add(new ProvinceBean("上海", 29)); //直辖市
// allProvinceList.add(new ProvinceBean("天津", 29)); //直辖市
// allProvinceList.add(new ProvinceBean("重庆", 29)); //直辖市
return allProvinceList;
}
/**
* 将数据存到文件里面
* @param context
* @param dataList
*/
public void saveStringData(Context context, List<String> dataList) {
Log.e(TAG, "");
String filePath = DateUtil.getLogPath(context);
File file = new File(filePath, "mydatastring.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
StringBuffer stringBuff = new StringBuffer();
for (String string : dataList) {
stringBuff.append(string);
}
OutputStream out = null;
try {
// ObjectOutputStream objOut = new ObjectOutputStream(out);
// objOut.writeObject(jsonString);
out = new FileOutputStream(file);
out.write(stringBuff.toString().getBytes());
out.close();
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}/*
* finally{ try { out.flush(); } catch (IOException e) { // TODO
* Auto-generated catch block e.printStackTrace(); } }
*/
getStringData(context);
}
public void getStringData(Context context) {
Log.e(TAG, "getData");
String filePath = DateUtil.getLogPath(context);
File file = new File(filePath, "mydatastring.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileInputStream fis;
try {
fis = new FileInputStream(file);
byte[] buf = new byte[1024];
StringBuffer sb = new StringBuffer();
while ((fis.read(buf)) != -1) {
sb.append(new String(buf));
buf = new byte[1024];// 重新生成,避免和上次读取的数据重复
}
fis.close();
String data = sb.toString();
String[] province = data.split("@");
List<CityBean> dataList = new ArrayList<CityBean>();
for (String string : province) {
String[] item = string.split("&");
CityBean cityBean = new CityBean();
for (int i = 0; i < item.length; i++) {
switch (i) {
case 0:
cityBean.setCityId(item[i]);
break;
case 1:
cityBean.setCountryName(item[i]);
break;
case 2:
cityBean.setProvinceName(item[i]);
break;
case 3:
cityBean.setCityName(item[i]);
break;
case 4:
cityBean.setCountyName(item[i]);
break;
case 5:
cityBean.setCountryNameInEnglish(item[i]);
break;
default:
break;
}
}
dataList.add(cityBean);
}
for (CityBean cityBean : dataList) {
Log.e("String解析数据", cityBean.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// public String convertToJSONString(List<CityBean> dataList) {
// Gson result = new Gson();
// return result.toJson(dataList);
// }
// public void savaData(Context context, List<CityBean> dataList) {
// Log.e(TAG, "");
// String jsonString = convertToJSONString(dataList);
// String filePath = DateUtil.getLogPath(context);
// File file = new File(filePath, "mydata.txt");
// if (!file.exists()) {
// try {
// file.createNewFile();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// OutputStream out = null;
// try {
// // ObjectOutputStream objOut = new ObjectOutputStream(out);
// // objOut.writeObject(jsonString);
// out = new FileOutputStream(file);
// out.write(jsonString.getBytes());
// out.close();
// out.flush();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }/*
// * finally{ try { out.flush(); } catch (IOException e) { // TODO
// * Auto-generated catch block e.printStackTrace(); } }
// */
//
// getData(context);
// }
// /**
// * 将本地的问价读出来并通过Gson转成集合对象(测试)
// * @param context
// */
// public void getData(Context context) {
// Log.e(TAG, "getData");
// String filePath = DateUtil.getLogPath(context);
// File file = new File(filePath, "mydata.txt");
// if (!file.exists()) {
// try {
// file.createNewFile();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// FileInputStream fis;
// try {
// fis = new FileInputStream(file);
// byte[] buf = new byte[1024];
// StringBuffer sb = new StringBuffer();
// while ((fis.read(buf)) != -1) {
// sb.append(new String(buf));
// buf = new byte[1024];// 重新生成,避免和上次读取的数据重复
// }
// fis.close();
// Gson result = new Gson();
// List<CityBean> ps = result.fromJson(sb.toString(),new TypeToken<List<CityBean>>() {}.getType());
// for (CityBean cityBean : ps) {
// Log.e("GSON解析数据", cityBean.toString());
// }
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// }
}