java 取商_Java读取GIS服务器数据

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 formparams = new ArrayList();

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 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 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.199999×××549}}}

解析一次之后的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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值