Java读取GIS服务器数据
2013-09-15 16:40
316 查看
package com.gis.action; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import net.sf.json.JSONObject; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.codehaus.jackson.map.ObjectMapper; import com.gis.entity.Attributes; import com.gis.entity.Geometry; import com.gis.entity.HeavyMetal; import com.gis.rest.RestAPI; import com.gis.util.Constants; public class QueryAction { public HeavyMetal queryByObjectid(int objectid) { HeavyMetal heavyMetal = new HeavyMetal(); List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("f", "json")); //queryurl=http://192.168.24.218/ArcGIS/rest/services/HeavyMetal/FeatureServer/0/11(11是objectid) String response = RestAPI.rest(Constants.queryurl + objectid, formparams); JSONObject jsonObject = JSONObject.fromObject(response); Map map = new HashMap(); for (Iterator iter = jsonObject.keys(); iter.hasNext();) { String key = (String) iter.next(); map.put(key, jsonObject.get(key)); } System.out.println("feature:" + map.get("feature")); ObjectMapper objectMapper = new ObjectMapper(); String str = map.get("feature").toString(); JSONObject jsonObject1 = JSONObject.fromObject(str); Map map1 = new HashMap(); String[] strings = new String[2]; int i = 0; for (Iterator iter = jsonObject1.keys(); iter.hasNext();) { // System.out.println(iter); String key = (String) iter.next(); map.put(key, jsonObject1.get(key)); // System.err.println(jsonObject1.get(key)); strings[i] = jsonObject1.get(key).toString(); i++; } JSONObject obj = new JSONObject().fromObject(strings[0]); // System.err.println("OBJECTID:" + obj.get("OBJECTID")); Attributes attributes = (Attributes) JSONObject.toBean(obj, Attributes.class); obj = new JSONObject().fromObject(strings[1]); Geometry geometry = (Geometry) JSONObject.toBean(obj, Geometry.class); heavyMetal.setAttributes(attributes); heavyMetal.setGeometry(geometry); return heavyMetal; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub QueryAction queryAction = new QueryAction(); System.err.println(queryAction.queryByObjectid(11)); } }
public class RestAPI { public static String rest(String url,List<NameValuePair> formparams) { HttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost(url); String responseBody = null; BufferedReader br = null; try { UrlEncodedFormEntity uef = new UrlEncodedFormEntity(formparams, HTTP.UTF_8); post.setEntity(uef); ResponseHandler<String> responseHandler = new BasicResponseHandler(); responseBody = httpClient.execute(post, responseHandler); System.out.println(responseBody); } catch (Exception e) { // TODO: handle exception } return responseBody; } }
response值
{"feature":{"attributes":{"OBJECTID":11,"ID":1,"CD":0.2433,"HG":0.2433,"DP_AS":1.0309,"CU":2.3848,"PB":217.214,"CR":353.527,"ZN":357.4679,"NI":208.2775,"HM_TIME":1379085429000,"HM_NAME":"宜昌","USER_NAME" : null},"geometry":{"x":111.300000000745,"y":31.1999999992549}}}
解析一次之后的str值
feature:{"attributes":{"OBJECTID":11,"ID":1,"CD":0.2433,"HG":0.2433,"DP_AS":1.0309,"CU":2.3848,"PB":217.214,"CR":353.527,"ZN":357.4679,"NI":208.2775,"HM_TIME":1379085429000,"HM_NAME":"宜昌","USER_NAME":null},"geometry":{"x":111.3,"y":31.2}}
再分别解析出attributes和geometry,最终得到解析出的对象
demo地址http://down.51cto.com/data/947003
本文出自 “优赛工作室” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1297385
相关文章推荐
- Java读取GIS服务器数据
- java SDK ,上传、读取 基本类型数据通过服务器(客户端代码)
- JAVA使用JeasyOPC读取OPC 服务器数据
- 用java在客户端读取mongodb中的数据并发送至服务器
- crontab java -jar --------- java.sql.SQLRecoverableException: 无法从套接字读取更多的数据
- java Applet从网页读取数据
- java httpURL连接远程服务器并返回数据(httpurlconnection)
- java InputStream读取数据问题
- 解如何利用 XML 和 JavaScript Object Notation 在 Ajax 客户端和 Java 服务器之间传输数据(代码)(Oracle)。
- 往服务器连续写7个字节的数据作为命令,但是javabyte类型的范围为-128-127。这样要转换
- java中读取src文件下属性文件(支持跨服务器)
- JAVA 读取 制定路径的 XML 文件 和 获取 服务器路径
- java 利用POI 读取Execel数据的真实行数
- 读取excel数据的java类
- JAVA读取Mysql数据的方法
- (JXL)JAVA读取Excel数据(包含多个Sheet)
- 通过java程序读取文件xml中的数据
- java中读取配置文件中的数据
- java的poi技术读取Excel数据
- (转)live555从RTSP服务器读取数据到使用接收到的数据流程分析