private void getAddress(){
Log.e("获取地址",".......");
/*StringBuilder sb = new StringBuilder();
AssetManager am = activity.getAssets();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
am.open("pro")));
String next = "";
while (null != (next = br.readLine())) {
sb.append(next);
}
} catch (IOException e) {
TODO Auto-generated catch block
e.printStackTrace();
sb.delete(0, sb.length());
}*/
将json文件读取到buffer数组中
InputStream is = this.getResources().openRawResource(R.raw.pro);
byte[] buffer = new byte[0];
try {
buffer = new byte[is.available()];
is.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
将字节数组转换为以utf-8编码的字符串
String json = null;
try {
json = new String(buffer, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Log.e("sb",json);
Log.e("sb",sb.toString());
JSONObject object = JsonUtil.convertJsonObj(json);
JSONObject object1=JsonUtil.getJsonObj(object,"data");
JSONArray JSONArrayprovence = JsonUtil.convertJsonArry(object1, "provence");
JSONArray JSONArraycity = JsonUtil.convertJsonArry(object1, "city");
JSONArray JSONArrayarea = JsonUtil.convertJsonArry(object1, "area");
Log.e("333", JSONArrayprovence.length() + "");
Log.e("11", JSONArrayarea.length() + "");
Log.e("222", JSONArraycity.length() + "");
XmlSerializer serializer = Xml.newSerializer();获取XML写入信息的序列化对象
File file = new File(Environment.getExternalStorageDirectory(), "provinces.xml");
try {
FileOutputStream outStream = new FileOutputStream(file,true);
serializer.setOutput(outStream, "UTF-8");
serializer.startDocument("UTF-8", true);
serializer.startTag("", "province");
for (int i = 0; i < JSONArrayprovence.length(); i++) {
JSONObject jsonObject=JsonUtil.convertJsonObj(JSONArrayprovence, i);
try {
serializer.startTag("", "Province");
serializer.attribute("", "provinceID", JsonUtil.getStr(jsonObject, "provinceID"));
serializer.attribute("", "province", JsonUtil.getStr(jsonObject, "province"));
serializer.attribute("", "father", JsonUtil.getStr(jsonObject, "father"));
serializer.text(JsonUtil.getStr(jsonObject, "province"));
serializer.endTag("", "Province");
} catch (IOException e) {
e.printStackTrace();
}
}
serializer.endTag("", "province");
serializer.endDocument();
serializer.flush();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
XmlSerializer serializercity = Xml.newSerializer();获取XML写入信息的序列化对象
File file1 = new File(Environment.getExternalStorageDirectory(), "cities.xml");
try {
FileOutputStream outStream = new FileOutputStream(file1,true);
serializercity.setOutput(outStream, "UTF-8");
serializercity.startDocument("UTF-8", true);
serializercity.startTag("", "city");
for (int i = 0; i < JSONArraycity.length(); i++) {
JSONObject jsonObject=JsonUtil.convertJsonObj(JSONArraycity, i);
try {
serializercity.startTag("", "City");
serializercity.attribute("", "city",JsonUtil.getStr(jsonObject, "city"));
serializercity.attribute("", "cityID", JsonUtil.getStr(jsonObject, "cityID"));
serializercity.attribute("", "father", JsonUtil.getStr(jsonObject, "father"));
serializercity.text(JsonUtil.getStr(jsonObject, "city"));
serializercity.endTag("", "City");
} catch (IOException e) {
e.printStackTrace();
}
}
serializercity.endTag("", "city");
serializercity.endDocument();
serializercity.flush();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
XmlSerializer serializerarea = Xml.newSerializer();获取XML写入信息的序列化对象
File file2 = new File(Environment.getExternalStorageDirectory(), "districts.xml");
try {
FileOutputStream outStream = new FileOutputStream(file2,true);
serializerarea.setOutput(outStream, "UTF-8");
serializerarea.startDocument("UTF-8", true);
serializerarea.startTag("", "district");
for (int i = 0; i < JSONArrayarea.length(); i++) {
JSONObject jsonObject= JsonUtil.convertJsonObj(JSONArrayarea, i);
try {
serializerarea.startTag("", "District");
serializerarea.attribute("", "area", JsonUtil.getStr(jsonObject,"area"));
serializerarea.attribute("", "areaID", JsonUtil.getStr(jsonObject, "areaID"));
serializerarea.attribute("", "father", JsonUtil.getStr(jsonObject, "father"));
serializerarea.text(JsonUtil.getStr(jsonObject, "area"));
serializerarea.endTag("", "District");
} catch (IOException e) {
e.printStackTrace();
}
}
serializerarea.endTag("", "district");
serializerarea.endDocument();
serializerarea.flush();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}