使用特定功能代碼
阿新 • • 發佈:2017-09-26
out als tac 所有 catch stat json buffere rds
1.通過高德api將我國省市區存入數據庫
1)用到的工具類
package com.ty.tyzxtj.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.View Codeparams.HttpMethodParams; /** * http工具類 * @author wangjiping * */ public class HttpUtil { // 超時時間 public static final int TIME_OUT = 50000; public static String post(String postUrl){ String response = ""; PostMethod postMethod = new PostMethod(postUrl); try { HttpClient client= new HttpClient(); client.getHttpConnectionManager().getParams() .setConnectionTimeout(50000);// 設置連接時間 int status = client.executeMethod(postMethod); if (status == HttpStatus.SC_OK) { InputStream inputStream = postMethod.getResponseBodyAsStream(); BufferedReader br= new BufferedReader(new InputStreamReader( inputStream)); StringBuffer stringBuffer = new StringBuffer(); String str = ""; while ((str = br.readLine()) != null) { stringBuffer.append(str); } response = stringBuffer.toString(); } else { response = "fail"; } } catch (Exception e) { e.printStackTrace(); } finally { // 釋放連接 postMethod.releaseConnection(); } return response; } public static String loadJson (String url) { StringBuilder json = new StringBuilder(); try { URL urlObject = new URL(url); URLConnection uc = urlObject.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream())); String inputLine = null; while ( (inputLine = in.readLine()) != null) { json.append(inputLine); } in.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return json.toString(); } public static String get(String url){ String html = ""; // 構造HttpClient的實例 HttpClient httpClient = new HttpClient(); // 創建GET方法的實例 GetMethod getMethod = new GetMethod(url); // 使用系統提供的默認的恢復策略 不重試 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); getMethod.setRequestHeader("Connection" , "Keep-Alive"); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(TIME_OUT); httpClient.getHttpConnectionManager().getParams().setSoTimeout(TIME_OUT); try { // 執行getMethod int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + getMethod.getStatusLine()); } // 處理內容 html = getMethod.getResponseBodyAsString(); } catch (Exception e) { } finally { // 釋放連接 getMethod.releaseConnection(); } return html; } }
2)關鍵代碼
public void autoGetAdCode(String gaodeApi) { try { String result1 = HttpUtil.get(gaodeApi); JSONObject result = JSONObject.parseObject(result1); JSONArray provinces =result.getJSONArray("districts").getJSONObject(0).getJSONArray("districts"); for (Object object : provinces) {//遍歷省 JSONObject province = JSONObject.parseObject(object.toString()); String name = province.getString("name"); String level = province.getString("level"); String adcode = province.getString("adcode"); String center = province.getString("center"); regionalStaMapper.insertDivistion(name,level,adcode,center); //省信息存入數據庫 for (Object object2 : province.getJSONArray("districts")) {//遍歷市 JSONObject city = JSONObject.parseObject(object2.toString()); //市信息存入數據庫 name = city.getString("name"); level = city.getString("level"); adcode = city.getString("adcode"); center = city.getString("center"); regionalStaMapper.insertDivistion(name,level,adcode,center); if(!level.equals("city")){ System.out.println(level); continue; } for (Object object3 : city.getJSONArray("districts")) {//遍歷區 JSONObject district = JSONObject.parseObject(object3.toString()); //區信息存入數據庫 name = district.getString("name"); level = district.getString("level"); adcode = district.getString("adcode"); center = district.getString("center"); regionalStaMapper.insertDivistion(name,level,adcode,center); } } } } catch (Exception e) { log.error(e.toString()); throw new RuntimeException(); } }View Code
@Insert("insert into ty_conf_division(adcode,center,level,name) values(#{adcode},#{center},#{level},#{name})") Integer insertDivistion(@Param("name")String name, @Param("level")String level, @Param("adcode")String adcode, @Param("center")String center);View Code
3)思路
通過高德地圖api:http://lbs.amap.com/api/webservice/guide/api/district中的
http://restapi.amap.com/v3/config/district?key=您的key&keywords=中國&subdistrict=3&extensions=base獲得所有數據
註意:可能出現的key需要驗證,出錯又查看提示的習慣
使用特定功能代碼