Java中調用ArcGIS Server REST API


前面寫過一篇博文<< 利用ArcGIS Server REST API實現對Feature的編輯操作>>,講述了Flex中如何調用ArcGIS的REST服務。這里我們來看一下Java中如何調用ArcGIS Server Rest API,同樣利用Esri提供的在線服務 http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/FeatureServer/0作為測試用例。
 
查詢功能的實現
在瀏覽器中輸入 http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/FeatureServer/0/query,在where中輸入1=1,Out Fields輸入*,點擊Query(GET)或者Query(POST)即可查看所有的記錄。
參考這部分API的 說明文檔 ,我們在Java中通過調用REST API來實現與上面的操作對應的查詢功能, 首先需要引入httpcomponents-client-4.2.1-bin.tar.gz中lib文件夾下的所有jar包,以支持HttpClient、 BasicNameValuePair等類。相關代碼如下:
public void query(){
        //創建一個http客戶端
        HttpClient client=new DefaultHttpClient();
        //創建一個POST請求  
        HttpPost request=new HttpPost("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/FeatureServer/0/query");
         //設置HTTP POST請求參數必須用NameValuePair  
        List<NameValuePair> params = new ArrayList<NameValuePair>();  
        params.add(new BasicNameValuePair("f", "json"));//format設置成json
        params.add(new BasicNameValuePair("where","1=1"));
        params.add(new BasicNameValuePair("outFields","*"));
         
        try {
            //設置http Post請求參數 
            HttpEntity entity = new UrlEncodedFormEntity(params);
            request.setEntity(entity); 
            HttpResponse response=client.execute(request);
            if(response.getStatusLine().getStatusCode()==200){//如果狀態碼為200,就是正常返回
                String result=EntityUtils.toString(response.getEntity());
                System.out.println(result);            
                //需要對返回的結果進行分析,判斷新增記錄是成功還是失敗
                //如果成功,則進行后續的分析
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            //進行處理操作            
        } catch (IOException e) {
            //進行處理操作
        }        
    }
 
新增功能的實現 
在瀏覽器中輸入http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/FeatureServer/0/addFeatures,Format選擇JSON,在Features中輸入[{"geometry":{"y":34,"x":108},"attributes":{"description":"addPoint","type":2}}],點擊AddFeatures即可新增記錄。
Java中實現新增功能的代碼和查詢功能基本相同,其難點在於構造出Features對應的JSON字符串。要在Java中實現上面的新增功能,可直接使用構造好的json字符串:
params.add(new BasicNameValuePair("features","[{'geometry':{'y':34,'x':108},'attributes':{'description':'addPoint','type':2}}]"));
但是考慮到靈活性,我們需要根據參數動態構造出JSON字符串:首先需引入 jackson-all-1.7.6 ,該工具能將對象轉換成json字符串,具體代碼如下:
/**根據參數構造生成json字符串
     * 示例:[{"geometry":{"y":0,"x":0},"attributes":{"description":"addPoint","type":2}}]     * 
     * */
    public String makeJson(){
        //構造空間數據
        Map<String,Object> geometry = new HashMap<String,Object>();
        geometry.put("x", "108");
        geometry.put("y", "34");
        
        //構造屬性數據
        Map<String,Object> attributes = new HashMap<String,Object>();
        attributes.put("description", "addpoint");
        attributes.put("type", "2");   
        
        //構造一個feature    
        Map<String,Object> feature = new HashMap<String,Object>();
        feature.put("geometry", geometry);
        feature.put("attributes", attributes);
        
        List<Map<String,Object>> features = new ArrayList<Map<String,Object>>(); 
        features.add(feature);
        
        //利用jackson工具將對象轉換成json字符串
        ObjectMapper mapper = new ObjectMapper();  
        String jsonStr = null;
        try {
            jsonStr = mapper.writeValueAsString(features);
        } catch (JsonGenerationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return jsonStr;
    }
下面給出新增函數的代碼,其中 params.add(   new   BasicNameValuePair( "features"   ,makeJson()));使用了前面構造生成的json字符串:
/**新增記錄
     * 示例:[{"geometry":{"y":0,"x":0},"attributes":{"description":"addPoint","type":2}}]     * 
     * *///
    public void addFeature(){
        //創建一個http客戶端
        HttpClient client=new DefaultHttpClient();
        //創建一個POST請求  
        HttpPost request=new HttpPost("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/FeatureServer/0/addFeatures");
        //設置HTTP POST請求參數必須用NameValuePair  
        List<NameValuePair> params = new ArrayList<NameValuePair>();  
        params.add(new BasicNameValuePair("f", "json"));//format設置成json
        //params.add(new BasicNameValuePair("features","[{'geometry':{'y':34,'x':108},'attributes':{'description':'addPoint','type':2}}]"));
        params.add(new BasicNameValuePair("features",makeJson()));
                
        try {
            //設置http Post請求參數 
            HttpEntity entity = new UrlEncodedFormEntity(params);
            request.setEntity(entity); 
            HttpResponse response=client.execute(request);
            if(response.getStatusLine().getStatusCode()==200){//如果狀態碼為200,就是正常返回
                String result=EntityUtils.toString(response.getEntity());
                System.out.println(result);            
                //需要對返回的結果進行分析,判斷新增記錄是成功還是失敗
                //如果成功,則進行后續的分析
            }
        } catch (ClientProtocolException e) {                            
            e.printStackTrace();
            //進行處理操作    
        } catch (IOException e) {
            //進行處理操作
        }            
    }
備注
  1. 點擊這里下載源碼,開發環境:eclipse-SDK-3.7.2-win32
  2. 同樣適用於Android開發,因ArcGIS Runtime for Android包含了前面提到的兩個依賴包,因此可以直接使用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM