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包含了前面提到的两个依赖包,因此可以直接使用。
posted @ 2013-02-01 16:41  esrixa  阅读(9612)  评论(0编辑  收藏  举报