Android編程實(shí)現(xiàn)根據(jù)經(jīng)緯度查詢地址并對獲取的json數(shù)據(jù)進(jìn)行解析的方法
本文實(shí)例講述了Android編程實(shí)現(xiàn)根據(jù)經(jīng)緯度查詢地址并對獲取的json數(shù)據(jù)進(jìn)行解析的方法。分享給大家供大家參考,具體如下:
第一步:根據(jù)指定的URL從google 服務(wù)器上獲得包含地址的json格式的數(shù)據(jù)(其還提供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;
}
傳入經(jīng)緯度作為參數(shù)
/**
* 根據(jù)經(jīng)緯度獲得地址
* @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數(shù)據(jù):
public static boolean parseAddressJSON(StringBuffer sb){
try {
if(sb != null){
JSONObject jsonAllData = new JSONObject(sb.toString());
/**
* 獲得一個長度為1的JSON數(shù)組,如:[{數(shù)據(jù)內(nèi)容}]
*/
String placemarkStr = jsonAllData.getString("Placemark");
/**
* 將placemarkStr數(shù)組類型字符串構(gòu)造成一個JSONArray對象
*/
JSONArray placemarkArray = new JSONArray(placemarkStr);
/**
* Placemark標(biāo)簽內(nèi)容是一個長度為1的數(shù)組,獲得數(shù)組的內(nèi)容并轉(zhuǎn)換成字符串
*/
String jsonDataPlacemarkStr = placemarkArray.get(0).toString();
/**
* 對上面得到的JSON數(shù)據(jù)類型的字符串(jsonDataPlacemarkStr)進(jìn)行解析
*/
JSONObject jsonDataPlacemark = new JSONObject(jsonDataPlacemarkStr);
/**
* 獲得標(biāo)簽AddressDetails的JSON數(shù)據(jù)
*/
String jsonAddressDetails = jsonDataPlacemark.getString("AddressDetails");
/**
* 對上面得到的JSON數(shù)據(jù)類型的字符串(jsonAddressDetails)進(jìn)行解析
*/
JSONObject jsonDataAddressJDetails = new JSONObject(jsonAddressDetails);
/**
* 獲得標(biāo)簽Country的JSON數(shù)據(jù)
*/
String jsonCountry = jsonDataAddressJDetails.getString("Country");
/**
* 對上面得到的JSON數(shù)據(jù)類型的字符串(jsonCountry)進(jìn)行解析
*/
JSONObject jsonDataCountry = new JSONObject(jsonCountry);
/**
* 對解析出來的感興趣的數(shù)據(jù)進(jìn)行封裝
*/
LewatekGPSAddress lewatekGPSAddress = new LewatekGPSAddress();
/**
* 設(shè)置CountryName
*/
lewatekGPSAddress.setCountryName(jsonDataCountry.getString("CountryName"));
/**
* 設(shè)置CountryNameCode
*/
lewatekGPSAddress.setCountryNameCode(jsonDataCountry.getString("CountryNameCode"));
/**
* 獲得標(biāo)簽AdministrativeArea的JSON數(shù)據(jù)
*/
String jsonAdministrativeArea = jsonDataCountry.getString("AdministrativeArea");
/**
* 對上面得到的JSON數(shù)據(jù)類型的字符串(jsonAdministrativeArea)進(jìn)行解析
*/
JSONObject jsonDataAdministrativeArea = new JSONObject(jsonAdministrativeArea);
/**
* 設(shè)置AdministrativeAreaName
*/
lewatekGPSAddress.setAdministrativeAreaName(jsonDataAdministrativeArea.getString("AdministrativeAreaName"));
/**
* 獲得標(biāo)簽Locality的JSON數(shù)據(jù)
*/
String jsonLocality = jsonDataAdministrativeArea.getString("Locality");
/**
* 對上面得到的JSON數(shù)據(jù)類型的字符串(jsonLocality)進(jìn)行解析
*/
JSONObject jsonDataLocality = new JSONObject(jsonLocality);
/**
* 設(shè)置LocalityName
*/
lewatekGPSAddress.setLocalityName(jsonDataLocality.getString("LocalityName"));
/**
* 獲得標(biāo)簽DependentLocality的JSON數(shù)據(jù)
*/
String jsonDependentLocality = jsonDataLocality.getString("DependentLocality");
/**
* 對上面得到的JSON數(shù)據(jù)類型的字符串(jsonDependentLocality)進(jìn)行解析
*/
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服務(wù)器上獲得的json數(shù)據(jù)(提取對我有用的數(shù)據(jù):CountryName、LocalityName、AdministrativeAreaName、DependentLocalityName,即中國上海市上海市浦東新區(qū)(中國湖南省衡陽市衡山縣這樣的數(shù)據(jù)也能提取)):
{
"name": "31.20322202833381,121.59876351250254",
"Status": {
"code": 200,
"request": "geocode"
},
"Placemark": [ {
"id": "p1",
"address": "中國上海市浦東新區(qū)祖沖之路994號-1088號",
"AddressDetails": {
"Accuracy" : 8,
"Country" : {
"AdministrativeArea" : {
"AdministrativeAreaName" : "上海市",
"Locality" : {
"DependentLocality" : {
"DependentLocalityName" : "浦東新區(qū)",
"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":"中國上海市浦東新區(qū)祖沖之路994號-1088號","Point":{"coordinates":[121.5986103,31.2038252,0]},"AddressDetails":{"Country":{"CountryNameCode":"CN","CountryName":"中國","AdministrativeArea":{"Locality":{"LocalityName":"上海市","DependentLocality":{"DependentLocalityName":"浦東新區(qū)","Thoroughfare":{"ThoroughfareName":"祖沖之路994號-1088號"}}},"AdministrativeAreaName":"上海市"}},"Accuracy":8}}] at Placemark of type org.json.JSONArray cannot be converted to JSONObject
PS:這里再為大家推薦幾款比較實(shí)用的json在線工具供大家參考使用:
在線JSON代碼檢驗、檢驗、美化、格式化工具:
http://tools.jb51.net/code/json
JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat
C語言風(fēng)格/HTML/CSS/json代碼格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android通過原生方式獲取經(jīng)緯度與城市信息的方法
- Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo)
- Android通過原生APi獲取所在位置的經(jīng)緯度
- Android獲取當(dāng)前位置的經(jīng)緯度數(shù)據(jù)
- android通過gps獲取定位的位置數(shù)據(jù)和gps經(jīng)緯度
- Android獲取經(jīng)緯度計算距離介紹
- Android 通過當(dāng)前經(jīng)緯度獲得城市的實(shí)例代碼
- android手機(jī)獲取gps和基站的經(jīng)緯度地址實(shí)現(xiàn)代碼
- Android獲取經(jīng)緯度的完美解決方案
相關(guān)文章
AndroidManifest.xml <uses-feature>和<uses-permisstio
這篇文章主要介紹了AndroidManifest.xml <uses-feature>和<uses-permisstion>分析及比較的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android CoordinatorLayout高級用法之自定義Behavior
這篇文章主要介紹了Android CoordinatorLayout高級用法之自定義Behavior,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02
Android實(shí)現(xiàn)圖片點(diǎn)擊預(yù)覽效果(zoom動畫)
本文主要介紹了Android實(shí)現(xiàn)圖片點(diǎn)擊預(yù)覽效果的方法步驟。具有很好的參考價值,下面跟著小編一起來看下吧2017-03-03
android網(wǎng)絡(luò)圖片查看器簡單實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了android網(wǎng)絡(luò)圖片查看器的簡單實(shí)現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
Android應(yīng)用圖標(biāo)在狀態(tài)欄上顯示實(shí)現(xiàn)原理
Android應(yīng)用圖標(biāo)在狀態(tài)欄上顯示,以及顯示不同的圖標(biāo),其實(shí)很研究完后,才發(fā)現(xiàn),很簡單,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06
詳解Android Studio中Git的配置及協(xié)同開發(fā)
這篇文章主要介紹了詳解Android Studio中Git的配置及協(xié)同開發(fā),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03

