Skip to content

Instantly share code, notes, and snippets.

@yung-yu
Created February 23, 2018 07:06
Show Gist options
  • Save yung-yu/8bbf8d46e1146a9e2f734f5397693af5 to your computer and use it in GitHub Desktop.
Save yung-yu/8bbf8d46e1146a9e2f734f5397693af5 to your computer and use it in GitHub Desktop.
台灣行政區域郵遞區號3碼
public static void getZipJsonFile(Context context){
try {
JSONObject result = new JSONObject();
String response = HttpProcessor.get(context, "https://api.opencube.tw/twzipcode/get-citys", 1);
Log.d(response);
JSONObject jsonObject = new JSONObject(response.trim());
JSONArray jsonArray = jsonObject.getJSONArray("data");
for(int i = 0; i< jsonArray.length(); i++){
String city = jsonArray.getString(i);
Log.d(city);
String res = HttpProcessor.get(context, "https://api.opencube.tw/twzipcode?city="+city, 1);
JSONObject areaCode = new JSONObject(res.trim());
JSONArray areaArray = areaCode.getJSONArray("data");
JSONArray item = new JSONArray();
for (int j = 0; j< areaArray.length(); j++){
JSONObject json = areaArray.getJSONObject(j);
JSONObject itemResult = new JSONObject();
itemResult.put("zip_code", json.optString("zip_code"));
itemResult.put("district", json.optString("district"));
item.put(itemResult);
}
Log.d(res);
result.put(city, item);
}
File file = new File(FileProcessor.getMessagePlusAppPath()+"zipCode.json");
if(file.exists()){
file.delete();
}
file.createNewFile();
FileWriter writer = new FileWriter(file.getAbsolutePath());
writer.write(result.toString());
} catch (Exception e) {
Log.e(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment