栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android编程实现根据经纬度查询地址并对获取的json数据进行解析的方法

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android编程实现根据经纬度查询地址并对获取的json数据进行解析的方法

本文实例讲述了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;
}

传入经纬度作为参数


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());
 
 String placemarkStr = jsonAllData.getString("Placemark");
 
 JSonArray placemarkArray = new JSonArray(placemarkStr);
 
 String jsonDataPlacemarkStr = placemarkArray.get(0).toString();
 
 JSonObject jsonDataPlacemark = new JSonObject(jsonDataPlacemarkStr);
 
 String jsonAddressDetails = jsonDataPlacemark.getString("AddressDetails");
 
 JSonObject jsonDataAddressJDetails = new JSonObject(jsonAddressDetails);
 
 String jsonCountry = jsonDataAddressJDetails.getString("Country");
 
 JSonObject jsonDataCountry = new JSonObject(jsonCountry);
 
 LewatekGPSAddress lewatekGPSAddress = new LewatekGPSAddress();
 
 lewatekGPSAddress.setCountryName(jsonDataCountry.getString("CountryName"));
 
 lewatekGPSAddress.setCountryNameCode(jsonDataCountry.getString("CountryNameCode"));
 
 String jsonAdministrativeArea = jsonDataCountry.getString("AdministrativeArea");
 
 JSonObject jsonDataAdministrativeArea = new JSonObject(jsonAdministrativeArea);
 
 lewatekGPSAddress.setAdministrativeAreaName(jsonDataAdministrativeArea.getString("AdministrativeAreaName"));
 
 String jsonLocality = jsonDataAdministrativeArea.getString("Locality");
 
 JSonObject jsonDataLocality = new JSonObject(jsonLocality);
 
 lewatekGPSAddress.setLocalityName(jsonDataLocality.getString("LocalityName"));
 
 String jsonDependentLocality = jsonDataLocality.getString("DependentLocality");
 
 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 JSonObject

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程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/159588.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号