Android編程實(shí)現(xiàn)根據(jù)經(jīng)緯度查詢地址并對(duì)獲取的json數(shù)據(jù)進(jìn)行解析的方法
本文實(shí)例講述了Android編程實(shí)現(xiàn)根據(jù)經(jīng)緯度查詢地址并對(duì)獲取的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());
/**
* 獲得一個(gè)長(zhǎng)度為1的JSON數(shù)組,如:[{數(shù)據(jù)內(nèi)容}]
*/
String placemarkStr = jsonAllData.getString("Placemark");
/**
* 將placemarkStr數(shù)組類型字符串構(gòu)造成一個(gè)JSONArray對(duì)象
*/
JSONArray placemarkArray = new JSONArray(placemarkStr);
/**
* Placemark標(biāo)簽內(nèi)容是一個(gè)長(zhǎng)度為1的數(shù)組,獲得數(shù)組的內(nèi)容并轉(zhuǎn)換成字符串
*/
String jsonDataPlacemarkStr = placemarkArray.get(0).toString();
/**
* 對(duì)上面得到的JSON數(shù)據(jù)類型的字符串(jsonDataPlacemarkStr)進(jìn)行解析
*/
JSONObject jsonDataPlacemark = new JSONObject(jsonDataPlacemarkStr);
/**
* 獲得標(biāo)簽AddressDetails的JSON數(shù)據(jù)
*/
String jsonAddressDetails = jsonDataPlacemark.getString("AddressDetails");
/**
* 對(duì)上面得到的JSON數(shù)據(jù)類型的字符串(jsonAddressDetails)進(jìn)行解析
*/
JSONObject jsonDataAddressJDetails = new JSONObject(jsonAddressDetails);
/**
* 獲得標(biāo)簽Country的JSON數(shù)據(jù)
*/
String jsonCountry = jsonDataAddressJDetails.getString("Country");
/**
* 對(duì)上面得到的JSON數(shù)據(jù)類型的字符串(jsonCountry)進(jìn)行解析
*/
JSONObject jsonDataCountry = new JSONObject(jsonCountry);
/**
* 對(duì)解析出來的感興趣的數(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");
/**
* 對(duì)上面得到的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");
/**
* 對(duì)上面得到的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");
/**
* 對(duì)上面得到的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ù)(提取對(duì)我有用的數(shù)據(jù):CountryName、LocalityName、AdministrativeAreaName、DependentLocalityName,即中國(guó)上海市上海市浦東新區(qū)(中國(guó)湖南省衡陽(yáng)市衡山縣這樣的數(shù)據(jù)也能提?。?/p>
{
"name": "31.20322202833381,121.59876351250254",
"Status": {
"code": 200,
"request": "geocode"
},
"Placemark": [ {
"id": "p1",
"address": "中國(guó)上海市浦東新區(qū)祖沖之路994號(hào)-1088號(hào)",
"AddressDetails": {
"Accuracy" : 8,
"Country" : {
"AdministrativeArea" : {
"AdministrativeAreaName" : "上海市",
"Locality" : {
"DependentLocality" : {
"DependentLocalityName" : "浦東新區(qū)",
"Thoroughfare" : {
"ThoroughfareName" : "祖沖之路994號(hào)-1088號(hào)"
}
},
"LocalityName" : "上海市"
}
},
"CountryName" : "中國(guó)",
"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":"中國(guó)上海市浦東新區(qū)祖沖之路994號(hào)-1088號(hào)","Point":{"coordinates":[121.5986103,31.2038252,0]},"AddressDetails":{"Country":{"CountryNameCode":"CN","CountryName":"中國(guó)","AdministrativeArea":{"Locality":{"LocalityName":"上海市","DependentLocality":{"DependentLocalityName":"浦東新區(qū)","Thoroughfare":{"ThoroughfareName":"祖沖之路994號(hào)-1088號(hào)"}}},"AdministrativeAreaName":"上海市"}},"Accuracy":8}}] at Placemark of type org.json.JSONArray cannot be converted to JSONObject
PS:這里再為大家推薦幾款比較實(shí)用的json在線工具供大家參考使用:
在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
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語(yǔ)言風(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ù)庫(kù)操作技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- 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)緯度計(jì)算距離介紹
- Android 通過當(dāng)前經(jīng)緯度獲得城市的實(shí)例代碼
- android手機(jī)獲取gps和基站的經(jīng)緯度地址實(shí)現(xiàn)代碼
- Android獲取經(jīng)緯度的完美解決方案
相關(guān)文章
Android應(yīng)用開發(fā)中使用GridView網(wǎng)格布局的代碼示例
GridView布局比較基礎(chǔ),可以取代已經(jīng)逐漸淡出人們視線的TableLayout,這里我們就來看一下Android應(yīng)用開發(fā)中使用GridView網(wǎng)格布局的代碼示例:2016-06-06
android studio3.0.1無(wú)法啟動(dòng)Gradle守護(hù)進(jìn)程的解決方法
這篇文章主要為大家詳細(xì)介紹了android studio3.0.1無(wú)法啟動(dòng)Gradle守護(hù)進(jìn)程的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android自定義標(biāo)尺滑動(dòng)選擇值效果
這篇文章主要為大家詳細(xì)介紹了Android自定義標(biāo)尺滑動(dòng)選擇值效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
android studio開發(fā)實(shí)現(xiàn)APP開機(jī)自啟動(dòng)
這篇文章主要為大家詳細(xì)介紹了android studio開發(fā)實(shí)現(xiàn)APP開機(jī)自啟動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android PopupWindow使用方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了Android PopupWindow使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
android 觸屏的震動(dòng)響應(yīng)接口調(diào)用方法
android 相關(guān)開發(fā)過程中,經(jīng)常會(huì)使用到觸屏的震動(dòng)響應(yīng)接口,為此本文列出以下方法,想要了解的朋友可以參考下2012-11-11
Android中EditText 設(shè)置 imeOptions 無(wú)效問題的解決方法
有時(shí)候我們需要在EditText 輸出完之后 需要在鍵盤出現(xiàn) 右下角變成“Go”或“前往 搜索時(shí);通常我們需要設(shè)置Android:imeOptions屬性,但是今天我發(fā)現(xiàn)設(shè)置了無(wú)效,下面給大家分享下解決方案2016-12-12
Android手勢(shì)ImageView三部曲 第一部
這篇文章主要為大家詳細(xì)介紹了Android手勢(shì)ImageView三部曲的第一部,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
解決Android popupWindow設(shè)置背景透明度無(wú)效的問題
這篇文章主要介紹了解決Android popupWindow設(shè)置背景透明度無(wú)效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08

