Java調用SuperMap iServerJava6R的REST API

來源:互聯網
上載者:User

最近總是碰到有人問Java如何訪問SuperMap iServerJava6R的服務,由於目前SuperMap iServerJava6R不支援RMI訪問訪問,所以考慮使用Java直接存取REST的API。

環境準備:

1、開發工具 MyEclipse,伺服器iServerJava6R

2、HttpComponents及Org.json類庫,第一個http協議的開源Java用戶端工具庫,org.json是Json庫,這兩個都在iServerJava6R依賴庫中,直接調用即可。如為所用類庫,可以直接在iServerJava6R安裝目錄下\webapps\iserver\WEB-INF\lib找到:

3、iServerJava6R的REST API的文檔

 

開發思路:

1、根據REST API構造參數類。

2、調用HttpComponents發送請求,解析結果

3、展現結果,一般使用地圖

 

實現一個簡單的吧,就調用distance,量算距離,上代碼吧:

1、參數類,distance介面需要兩個參數一個是Point2D數組,另外一個是一個枚舉單位,我一般習慣使用以米為單位,所以直接用單位METER了。

參數類/** * 參數類 * @author awei20081 * */public class Point2D {   private double x ;   private double y;   public Point2D(double x,double y){  this.x = x;  this.y =y;   }public double getX() {return x;}public void setX(double x) {this.x = x;}public double getY() {return y;}public void setY(double y) {this.y = y; }}

2、發送請求與解析,這裡distance介面是一個get請求,所以構造調用HttpComponents的HttpGet對象,代碼如下:

請求發出與解析import java.net.URL;import java.net.URLEncoder;import java.util.ArrayList;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import org.json.JSONObject;public class getDemo {public final static void main(String[] args) throws Exception {//構建請求的URLString url="http://localhost:8090/iserver/services/map-world/rest/maps/%E4%B8%96%E7%95%8C%E5%9C%B0%E5%9B%BE/distance.rjson?";//構造參數ArrayList<Point2D> arrayList = new ArrayList<Point2D>();arrayList.add(new Point2D(0,0));arrayList.add(new Point2D(10,10));//GET的URL,參數直接加URL後URL uri = new URL(url+URLEncoder.encode("point2Ds="+toJSONArray(arrayList),"UTF-8")+"&unit=METER");HttpGet httpget=new HttpGet(uri.toString());//執行http的get請求HttpResponse response=new DefaultHttpClient().execute(httpget);//發送GET,並返回一個HttpResponse對象,相對於POST,省去了添加NameValuePair數組作參數if(response.getStatusLine().getStatusCode()==200){//如果狀態代碼為200,就是正常返回String result=EntityUtils.toString(response.getEntity());//得到返回的字串JSONObject jsresult = new JSONObject(result); System.out.println("長度為"+jsresult.get("distance")+"  "+jsresult.getString("unit"));//列印輸出}         }public static StringBuilder toJSONArray(ArrayList<Point2D> arrayList){StringBuilder string = new StringBuilder();string.append("[");for (int i = 0; i < arrayList.size()-1; i++) {JSONObject jsObject = new JSONObject(arrayList.get(i));string.append(jsObject.toString()+",");}JSONObject jsObject = new JSONObject(arrayList.get(arrayList.size()-1));string.append(jsObject.toString());string.append("]");return string;}}

備忘:這個地方比較糾結,我第一用HttpComponents碰到比較多的問題列舉如下閱讀時候可以參考一下:

(1)url可以直接擷取,即http://localhost:8090/iserver/services/map-world/rest/maps/世界地圖/distance.rjson,但是不能直接用,因為需要對中文進行編碼,而且不能直接調用URLEncoder直接對這個url整體進行編碼,此處我圖簡單直接對 世界地圖 進行編碼為:/%E4%B8%96%E7%95%8C%E5%9C%B0%E5%9B%BE,所以url為http://localhost:8090/iserver/services/map-world/rest/maps/%E4%B8%96%E7%95%8C%E5%9C%B0%E5%9B%BE/distance.rjson,可以逐段解析url進行編碼。

(2)、如果直接用org.json類庫中JSONArray構造解析Point2D數組,解析結果不太對,所以我直接構造一個toJSONArray來對Point2D數組進行處理。
(3)、預設瀏覽器對url進行編碼處理時候使用的是utf-8編碼,而且iServerJava6R對請求返回結果的編碼為utf-8。

3、結果展示,此處偷懶了直接調用println函數輸出。

長度為1570277.6641936298  METER

 

上面只是一個簡單的思路介紹,也許對高手很簡單,但是畢竟初次學習,記錄下來以備後查吧。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.