【Salesfoece】Apex基礎資料型別
此文轉載自:https://blog.csdn.net/tangthh123/article/details/106176769
前言:
我們現在需要做的功能是根據ip地址獲取當前地理位置,是Web 服務API,獲取後的位置資訊一般是城市級別的,不是很精確的那種獲取。
(1)開發者需要做的準備工作:
需要登入百度地圖的api,點選web 服務API.
開發文件的地址:http://lbsyun.baidu.com/index.php?title=webapi/ip-api
請求的URL 有倆個,
http://api.map.baidu.com/location/ip?ak=您的AK&ip=您的IP&coor=bd09ll //HTTP協議 https://api.map.baidu.com/location/ip?ak=您的AK&ip=您的IP&coor=bd09ll //HTTPS協議
請求的引數
其中需要注意的是: ak 是必填的,其他幾個引數可選。
(2)如何申請AK
地址:http://lbsyun.baidu.com/apiconsole/key#/home
首先需要點到我的應用裡面,新建應用,我這下面是已經建好了。
點選確定即可。
需要注意的是:ip白名單不建議使用 0.0.0.0/0 可以使用具體的ip名稱。
(3)程式碼呼叫
獲取到的json 我把他複製出來,看一下怎麼解析這個json. 我這裡用的是阿里巴巴下的jsonObject ,所以需要引用jar 包。
{ "address": "CN|上海|上海|None|CHINANET|0|0", "content": { "address_detail": { "province": "上海市", "city": "上海市", "street": "", "district": "", "street_number": "", "city_code": 289 }, "address": "上海市", "point": { "x": "13524118.26", "y": "3642780.37" } }, "status": 0 }
(4)程式碼
package com.bos.test; import com.alibaba.fastjson.JSONObject; import com.bos.util.SendRequest; public class MenuTest { public static void main(String[] args) { String url = "http://api.map.baidu.com/location/ip?&ak=lGhonqkkoUGZurajdlpL1QqciA215kkq"; JSONObject jsonObject = SendRequest.sendGet2(url); System.out.println(jsonObject); //獲取content中的值 JSONObject contentJsonObject = jsonObject.getJSONObject("content"); System.out.println("contentJsonObject-------"+contentJsonObject); //獲取詳細地址資料 JSONObject addressDetailJsonObject = contentJsonObject.getJSONObject("address_detail"); String address = addressDetailJsonObject.getString("province"); String city = addressDetailJsonObject.getString("city"); System.out.println("address_detail-------"+addressDetailJsonObject); System.out.println("address-------"+address); System.out.println("city-------"+city); String address2 = contentJsonObject.getString("address"); System.out.println("address2-----"+address2); JSONObject pointJsonObject = contentJsonObject.getJSONObject("point"); String x = pointJsonObject.getString("x"); String y = pointJsonObject.getString("y"); System.out.println("x----"+x); System.out.println("y----"+y); } }
/**
* 傳送get 請求,比如定時任務呼叫的就是請求
* @param url
* @return
*/
public static JSONObject sendGet2(String url) {
JSONObject jsonObject = null;
StringBuffer sb = new StringBuffer();
BufferedReader in = null;
try {
String urlName = url;
URL realUrl = new URL(urlName);
// 開啟和URL之間的連線
URLConnection conn = realUrl.openConnection();
// 設定通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn.setConnectTimeout(10000);
// 建立實際的連線
conn.connect();
// 定義BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
jsonObject = JSON.parseObject(sb.toString());
} catch (Exception e) {
System.out.println("傳送GET請求出現異常!" + e);
// 使用finally塊來關閉輸入流
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
System.out.println("關閉流異常");
}
}
return jsonObject;
}
做到上面其實就可以了,但是如果呼叫次數太多的話也會報異常。這個時候就需要申請開發者資質認證,我這邊先申請一下個人開發者。
番外加贈:
上面的文件可以獲取到當前城市的中心點,比如我在上海 ,獲取的就是上海市等。 如果想要精確資訊,可以根據返回的經緯度做一步查詢。
文件地址:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding-abroad
其中需要注意的就是要查詢自己是否有逆地理編碼許可權,具體操作圖在下面。、
查詢應用是否有逆地理編碼的許可權。像我的話,沒有,點選申請許可權。
點選申請許可權往下做下去,我這邊就不做了