通过百度地图模糊查询获取详细地址?正则匹配
生活随笔
收集整理的這篇文章主要介紹了
通过百度地图模糊查询获取详细地址?正则匹配
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
要求:獲取地址的詳情?
地圖:百度地圖
準備:你的AK(地址:http://lbsyun.baidu.com/index.php?title=%E9%A6%96%E9%A1%B5),只需要注冊申請就可以了。
HttpURLConnectionExample類碼:
package com.qiao.example.concurrency.example.controller;import lombok.extern.slf4j.Slf4j;import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern;import javax.net.ssl.HttpsURLConnection;@Slf4j public class HttpURLConnectionExample {private final String USER_AGENT = "Mozilla/5.0";// HTTP POST請求void sendPost(String url) throws Exception {URL realUrl= new URL(null, url, new sun.net.www.protocol.https.Handler());HttpsURLConnection con= (HttpsURLConnection) realUrl.openConnection();//添加請求頭con.setRequestMethod("POST");con.setRequestProperty("User-Agent", USER_AGENT);con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";//發送Post請求con.setDoOutput(true);DataOutputStream wr = new DataOutputStream(con.getOutputStream());wr.writeBytes(urlParameters);wr.flush();wr.close();int responseCode = con.getResponseCode();System.out.println("\nSending 'POST' request to URL : " + url);System.out.println("Post parameters : " + urlParameters);System.out.println("Response Code : " + responseCode);BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));String inputLine;StringBuffer response = new StringBuffer();while ((inputLine = in.readLine()) != null) {response.append(inputLine);}in.close();// 通過json串獲取詳細地址String newAddress = json_getAddress(response.toString());System.out.println("打印輸出結果:"+newAddress);}/** 通過JSON串獲取詳細地址** (其實只要兩個正則就可以了,但是一時沒想到)*/public static String json_getAddress(String response){// log.info(response);String z_z = "(\"address\":\")[\\u4e00-\\u9fa5]+.*?(\")"; //匹配JSON中address字段及內容String newAddress = matchReg(response,z_z);log.info("String:{}",newAddress);String z_z1 = ":\"(.*)\""; //address內容 ("addess":"*****"--->:"*****")newAddress = matchReg(newAddress,z_z1);log.info("newAddress3:{}",newAddress);newAddress = newAddress.substring(2,newAddress.length()-1); //截取詳細地址內容(:"*****"--->*****)return newAddress;}/** 調用正則表達式* */public static String matchReg(String str, String pattern) {Pattern r = Pattern.compile(pattern);Matcher m = r.matcher(str);if (m.find()) {return m.group();} else {return "";}}}主類:
package com.qiao.example.concurrency.example.controller;import lombok.extern.slf4j.Slf4j;import java.util.regex.Matcher; import java.util.regex.Pattern;@Slf4j public class DetailedAddress {/** 輸入地址經過百度地圖之后輸出詳細地址** @param String address 模糊地址[區市、地址]* @return String address 詳細地址* */public static String detailed_address(String key){String url = "http://api.map.baidu.com/place/v2/search?query=key®ion=city&output=json&ak=你的AK";//AK需要替換成你自己的url = url.replace("key",key); //添加新地址(模糊地址)url = url.replace("city","廈門"); //需要檢索地址的上一級(默認廈門)return url;}/** 調用正則表達式* */public static String matchReg(String str, String pattern) {Pattern r = Pattern.compile(pattern);Matcher m = r.matcher(str);if (m.find()) {return m.group();} else {return "";}}public static void main(String[] args) throws Exception {String key = "廈大大學";// log.info("Http:",(key));String address_url = detailed_address(key); // 替換為新的連接// 訪問Http請求HttpURLConnectionExample http = new HttpURLConnectionExample();http.sendPost(address_url);} }歡迎評論,指點。
總結
以上是生活随笔為你收集整理的通过百度地图模糊查询获取详细地址?正则匹配的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 错误处理
- 下一篇: 堆溢出-House of orange