Android编程实现根据经纬度查询地址并对获取的json数据进行解析的方法
本文实例讲述了Android编程实现根据经纬度查询地址并对获取的json数据进行解析的方法。分享给大家供大家参考,具体如下:
第一步:根据指定的URL从google服务器上获得包含地址的json格式的数据(其还提供xml格式的,但json解析效率比xml高)
privatestaticStringBuffergetJSONData(StringurlPath){
try{
URLurl=newURL(urlPath);
HttpURLConnectionhttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setRequestMethod("GET");
if(httpURLConnection.getResponseCode()==200){
InputStreaminputStream=httpURLConnection.getInputStream();
InputStreamReaderisr=newInputStreamReader(inputStream);
BufferedReaderbr=newBufferedReader(isr);
Stringtemp=null;
StringBufferjsonsb=newStringBuffer();
while((temp=br.readLine())!=null){
jsonsb.append(temp);
}
returnjsonsb;
}
}catch(MalformedURLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnnull;
}
传入经纬度作为参数
/**
*根据经纬度获得地址
*@paramlatitude
*@paramlongitude
*@return
*/
publicstaticStringBuffergetCurrentAddressByGPS(longlatitude,longlongitude){
StringBufferstringBuffer=newStringBuffer();
stringBuffer.append(GOOGLE_GPS_PREFIX).append(latitude).append(",")
.append(longitude).append(GOOGLE_GPS_SUFFIX);
returngetJSONData(stringBuffer.toString());
}
第三,解析json数据:
publicstaticbooleanparseAddressJSON(StringBuffersb){
try{
if(sb!=null){
JSONObjectjsonAllData=newJSONObject(sb.toString());
/**
*获得一个长度为1的JSON数组,如:[{数据内容}]
*/
StringplacemarkStr=jsonAllData.getString("Placemark");
/**
*将placemarkStr数组类型字符串构造成一个JSONArray对象
*/
JSONArrayplacemarkArray=newJSONArray(placemarkStr);
/**
*Placemark标签内容是一个长度为1的数组,获得数组的内容并转换成字符串
*/
StringjsonDataPlacemarkStr=placemarkArray.get(0).toString();
/**
*对上面得到的JSON数据类型的字符串(jsonDataPlacemarkStr)进行解析
*/
JSONObjectjsonDataPlacemark=newJSONObject(jsonDataPlacemarkStr);
/**
*获得标签AddressDetails的JSON数据
*/
StringjsonAddressDetails=jsonDataPlacemark.getString("AddressDetails");
/**
*对上面得到的JSON数据类型的字符串(jsonAddressDetails)进行解析
*/
JSONObjectjsonDataAddressJDetails=newJSONObject(jsonAddressDetails);
/**
*获得标签Country的JSON数据
*/
StringjsonCountry=jsonDataAddressJDetails.getString("Country");
/**
*对上面得到的JSON数据类型的字符串(jsonCountry)进行解析
*/
JSONObjectjsonDataCountry=newJSONObject(jsonCountry);
/**
*对解析出来的感兴趣的数据进行封装
*/
LewatekGPSAddresslewatekGPSAddress=newLewatekGPSAddress();
/**
*设置CountryName
*/
lewatekGPSAddress.setCountryName(jsonDataCountry.getString("CountryName"));
/**
*设置CountryNameCode
*/
lewatekGPSAddress.setCountryNameCode(jsonDataCountry.getString("CountryNameCode"));
/**
*获得标签AdministrativeArea的JSON数据
*/
StringjsonAdministrativeArea=jsonDataCountry.getString("AdministrativeArea");
/**
*对上面得到的JSON数据类型的字符串(jsonAdministrativeArea)进行解析
*/
JSONObjectjsonDataAdministrativeArea=newJSONObject(jsonAdministrativeArea);
/**
*设置AdministrativeAreaName
*/
lewatekGPSAddress.setAdministrativeAreaName(jsonDataAdministrativeArea.getString("AdministrativeAreaName"));
/**
*获得标签Locality的JSON数据
*/
StringjsonLocality=jsonDataAdministrativeArea.getString("Locality");
/**
*对上面得到的JSON数据类型的字符串(jsonLocality)进行解析
*/
JSONObjectjsonDataLocality=newJSONObject(jsonLocality);
/**
*设置LocalityName
*/
lewatekGPSAddress.setLocalityName(jsonDataLocality.getString("LocalityName"));
/**
*获得标签DependentLocality的JSON数据
*/
StringjsonDependentLocality=jsonDataLocality.getString("DependentLocality");
/**
*对上面得到的JSON数据类型的字符串(jsonDependentLocality)进行解析
*/
JSONObjectjsonDataDependentLocality=newJSONObject(jsonDependentLocality);
lewatekGPSAddress.setDependentLocalityName(jsonDataDependentLocality.getString("DependentLocalityName"));
Log.e(TAG,lewatekGPSAddress.toString());
returntrue;
}
}catch(JSONExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnfalse;
}
从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}}]atPlacemarkoftypeorg.json.JSONArraycannotbeconvertedtoJSONObject
PS:这里再为大家推荐几款比较实用的json在线工具供大家参考使用:
在线JSON代码检验、检验、美化、格式化工具:
http://tools.jb51.net/code/json
JSON在线格式化工具:
http://tools.jb51.net/code/jsonformat
在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson
json代码在线格式化/美化/压缩/编辑/转换工具:
http://tools.jb51.net/code/jsoncodeformat
C语言风格/HTML/CSS/json代码格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android编程之activity操作技巧总结》、《Android文件操作技巧汇总》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。