本文實例講述了Android編程實現根據經緯度查詢地址并對獲取的json數據進行解析的方法。分享給大家供大家參考,具體如下:
第一步:根據指定的URL從google 服務器上獲得包含地址的json格式的數據(其還提供xml格式的,但json解析效率比xml高)
private static StringBuffer getJSONData(String urlPath){ try { URL url = new URL(urlPath); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setReadTimeout(5000); httpURLConnection.setRequestMethod("GET"); if(httpURLConnection.getResponseCode() == 200){ InputStream inputStream = httpURLConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(inputStream); BufferedReader br = new BufferedReader(isr); String temp = null; StringBuffer jsonsb = new StringBuffer(); while((temp = br.readLine()) != null){ jsonsb.append(temp); } return jsonsb; } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null;}傳入經緯度作為參數
/*** 根據經緯度獲得地址* @param latitude* @param longitude* @return*/public static StringBuffer getCurrentAddressByGPS(long latitude,long longitude){ StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(GOOGLE_GPS_PREFIX).append(latitude).append(",") .append(longitude).append(GOOGLE_GPS_SUFFIX); return getJSONData(stringBuffer.toString());}第三,解析json數據:
public static boolean parseAddressJSON(StringBuffer sb){ try { if(sb != null){ JSONObject jsonAllData = new JSONObject(sb.toString()); /** * 獲得一個長度為1的JSON數組,如:[{數據內容}] */ String placemarkStr = jsonAllData.getString("Placemark"); /** * 將placemarkStr數組類型字符串構造成一個JSONArray對象 */ JSONArray placemarkArray = new JSONArray(placemarkStr); /** * Placemark標簽內容是一個長度為1的數組,獲得數組的內容并轉換成字符串 */ String jsonDataPlacemarkStr = placemarkArray.get(0).toString(); /** * 對上面得到的JSON數據類型的字符串(jsonDataPlacemarkStr)進行解析 */ JSONObject jsonDataPlacemark = new JSONObject(jsonDataPlacemarkStr); /** * 獲得標簽AddressDetails的JSON數據 */ String jsonAddressDetails = jsonDataPlacemark.getString("AddressDetails"); /** * 對上面得到的JSON數據類型的字符串(jsonAddressDetails)進行解析 */ JSONObject jsonDataAddressJDetails = new JSONObject(jsonAddressDetails); /** * 獲得標簽Country的JSON數據 */ String jsonCountry = jsonDataAddressJDetails.getString("Country"); /** * 對上面得到的JSON數據類型的字符串(jsonCountry)進行解析 */ JSONObject jsonDataCountry = new JSONObject(jsonCountry); /** * 對解析出來的感興趣的數據進行封裝 */ LewatekGPSAddress lewatekGPSAddress = new LewatekGPSAddress(); /** * 設置CountryName */ lewatekGPSAddress.setCountryName(jsonDataCountry.getString("CountryName")); /** * 設置CountryNameCode */ lewatekGPSAddress.setCountryNameCode(jsonDataCountry.getString("CountryNameCode")); /** * 獲得標簽AdministrativeArea的JSON數據 */ String jsonAdministrativeArea = jsonDataCountry.getString("AdministrativeArea"); /** * 對上面得到的JSON數據類型的字符串(jsonAdministrativeArea)進行解析 */ JSONObject jsonDataAdministrativeArea = new JSONObject(jsonAdministrativeArea); /** * 設置AdministrativeAreaName */ lewatekGPSAddress.setAdministrativeAreaName(jsonDataAdministrativeArea.getString("AdministrativeAreaName")); /** * 獲得標簽Locality的JSON數據 */ String jsonLocality = jsonDataAdministrativeArea.getString("Locality"); /** * 對上面得到的JSON數據類型的字符串(jsonLocality)進行解析 */ JSONObject jsonDataLocality = new JSONObject(jsonLocality); /** * 設置LocalityName */ lewatekGPSAddress.setLocalityName(jsonDataLocality.getString("LocalityName")); /** * 獲得標簽DependentLocality的JSON數據 */ String jsonDependentLocality = jsonDataLocality.getString("DependentLocality"); /** * 對上面得到的JSON數據類型的字符串(jsonDependentLocality)進行解析 */ JSONObject jsonDataDependentLocality = new JSONObject(jsonDependentLocality); lewatekGPSAddress.setDependentLocalityName(jsonDataDependentLocality.getString("DependentLocalityName")); Log.e(TAG,lewatekGPSAddress.toString()); return true; } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false;}從google服務器上獲得的json數據(提取對我有用的數據:CountryName、LocalityName、AdministrativeAreaName、DependentLocalityName,即中國上海市上海市浦東新區(中國湖南省衡陽市衡山縣這樣的數據也能提取)):
{ "name": "31.20322202833381,121.59876351250254", "Status": { "code": 200, "request": "geocode" }, "Placemark": [ { "id": "p1", "address": "中國上海市浦東新區祖沖之路994號-1088號", "AddressDetails": { "Accuracy" : 8, "Country" : { "AdministrativeArea" : { "AdministrativeAreaName" : "上海市", "Locality" : { "DependentLocality" : { "DependentLocalityName" : "浦東新區", "Thoroughfare" : { "ThoroughfareName" : "祖沖之路994號-1088號" } }, "LocalityName" : "上海市" } }, "CountryName" : "中國", "CountryNameCode" : "CN" }}, "ExtendedData": { "LatLonBox": { "north": 31.2070152, "south": 31.2007199, "east": 121.6018752, "west": 121.5955799 } }, "Point": { "coordinates": [ 121.5986103, 31.2038252, 0 ] } } ]}Value [{"id":"p1","ExtendedData":{"LatLonBox":{"south":31.2007199,"west":121.5955799,"east":121.6018752,"north":31.2070152}},"address":"中國上海市浦東新區祖沖之路994號-1088號","Point":{"coordinates":[121.5986103,31.2038252,0]},"AddressDetails":{"Country":{"CountryNameCode":"CN","CountryName":"中國","AdministrativeArea":{"Locality":{"LocalityName":"上海市","DependentLocality":{"DependentLocalityName":"浦東新區","Thoroughfare":{"ThoroughfareName":"祖沖之路994號-1088號"}}},"AdministrativeAreaName":"上海市"}},"Accuracy":8}}] at Placemark of type org.json.JSONArray cannot be converted to JSONObjectPS:這里再為大家推薦幾款比較實用的json在線工具供大家參考使用:
在線JSON代碼檢驗、檢驗、美化、格式化工具:
http://tools.VeVB.COm/code/json
JSON在線格式化工具:
http://tools.VeVB.COm/code/jsonformat
在線XML/JSON互相轉換工具:
http://tools.VeVB.COm/code/xmljson
json代碼在線格式化/美化/壓縮/編輯/轉換工具:
http://tools.VeVB.COm/code/jsoncodeformat
C語言風格/HTML/CSS/json代碼格式化美化工具:
http://tools.VeVB.COm/code/ccode_html_css_json
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android操作json格式數據技巧總結》、《Android數據庫操作技巧總結》、《Android編程之activity操作技巧總結》、《Android文件操作技巧匯總》、《Android開發入門與進階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
新聞熱點
疑難解答