欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程實(shí)現(xiàn)根據(jù)經(jīng)緯度查詢地址并對獲取的json數(shù)據(jù)進(jìn)行解析的方法

 更新時間:2017年02月28日 11:31:53   作者:Jacob-wj  
這篇文章主要介紹了Android編程實(shí)現(xiàn)根據(jù)經(jīng)緯度查詢地址并對獲取的json數(shù)據(jù)進(jìn)行解析的方法,結(jié)合實(shí)例形式分析了Android的經(jīng)緯度地址解析與json格式數(shù)據(jù)操作相關(guā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ù)也能提?。?/p>

{
 "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代碼檢驗(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語言風(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è)計有所幫助。

相關(guān)文章

最新評論