后台解析rest服务的json数据

public List<String> getInitJsonDataForVD(String index, String type,
			String url, String layerName, int begin, int end, String sqlWhere,
			boolean flag) throws ParseException {
		List<String> list = new ArrayList<String>();

		try {
			// es中的字段属性
			List<String> esList = this.getFields(index, type);
			String dataUrl = HttpConnection.layerInfoUrl(url, layerName, begin,
					end, sqlWhere);
			String data = HttpConnection.getWebContent(dataUrl);
			JSONArray noArr = (JSONArray) JSON.parse(data);
			for (int i = 0; i < noArr.size(); i++) {
				JSONObject jobj = (JSONObject) noArr.get(i);
				String strFields = jobj.getString("fields");
				String temp = jobj.get("rowList").toString();
				java.util.Hashtable<String, String> itemData = new java.util.Hashtable<String, String>();
				String[] fieldArray = strFields.split(",");
				int fieldcount = fieldArray.length;
				for (int fieldI = 0; fieldI < fieldcount; fieldI++) {
					String colName = fieldArray[fieldI];
					// 取消本次操作,不记录 shape
					if (colName.toUpperCase().equals("SHAPE")) {
						continue;
					}
					// 取消本次操作,字段名称不能重复
					if (itemData.containsKey(colName)) {
						continue;
					}
					// 记录下字段名称,缺省值全部为空字符串
					itemData.put(colName, "");
				}

				JSONArray rowList = (JSONArray) JSON.parse(temp);

				for (int j = 0; j < rowList.size(); j++) {
					try {
						JSONObject cluster = (JSONObject) rowList.get(j);
						JSONObject temp2 = (JSONObject) cluster.get("values");
						// 取消本次操作,shape必须要存在
						if (temp2.containsKey("SHAPE") == false) {
							continue;
						}
						String SHAPE = (String) temp2.get("SHAPE");
						Geometry g1 = new WKTReader().read(SHAPE);
						// GeometryJSON 传递的是精度的位数
						GeometryJSON tempGeoJsons = new GeometryJSON(20);
						String strJson = tempGeoJsons.toString(g1);
						Enumeration<String> en1 = itemData.keys();
						while (en1.hasMoreElements()) {
							String colName2 = en1.nextElement();
							// 取消本次操作,字段名称不存在
							if (temp2.containsKey(colName2) == false) {
								continue;
							}
							Object colData2 = temp2.get(colName2);
							String colData3 = colData2 == null ? "" : colData2
									.toString();

							// 对字段进行判断
							if (flag == true) {
								itemData.put(colName2, colData3);
							} else {
								// 更新数据
								System.out.println("服务更新数据是否存在字段:"+colName2);
								if (this.isExistFiled(esList, colName2)) {
									itemData.put(colName2, colData3);
								}
							}

						}
						// 转换成es需要的json格式数据形式
						String tempData = JsonUtil.obj2JsonGeometryData2(
								itemData, strJson);
						if (tempData != null && tempData.length() > 0) {
							list.add(tempData);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return list;
		}
		return list;
	}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class HttpConnection {

	/**
	 * @throws UnsupportedEncodingException
	 * 
	 * @Title: layerInfoUrl
	 * @Description: 获取数据
	 * @param @param layer
	 * @param @param beginRecord
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public static String layerInfoUrl(String url, String layer,
			int beginRecord, int endRecord, String sqlWhere)
			throws UnsupportedEncodingException {

		String rtnUrl = url + "/mapData?serviceMethod=search&layerName="
				+ layer + "&colList=all&beginRecord=" + beginRecord
				+ "&featureLimit=" + endRecord
				+ "&returnCoordType=-1&returnType=1";

		// rtnUrl = rtnUrl + "&featureLimit=10";
		// sqlWhere=
		if (sqlWhere != null && sqlWhere.length() > 0) {
			String tempWhere = sqlWhere;
			tempWhere = java.net.URLEncoder.encode(tempWhere, "utf-8");
			tempWhere = java.net.URLEncoder.encode(tempWhere, "utf-8");
			rtnUrl = rtnUrl + "&sqlWhere=" + tempWhere;
		}
		// System.out.print(rtnUrl);
		return rtnUrl;
	}

	/**
	 * 
	 * @Title: getRestContent
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param urlString
	 * @param @param charset
	 * @param @param timeout
	 * @param @return
	 * @param @throws IOException 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public static String getRestContent(String urlString, String parameter)
			throws IOException {
		if (urlString == null || urlString.length() == 0) {
			return null;
		}
		urlString = (urlString.startsWith("http://") || urlString
				.startsWith("https://")) ? urlString : ("http://" + urlString)
				.intern();
		URL url = new URL(urlString);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		conn.setRequestMethod("POST");
		conn.setRequestProperty("Content-Type", "application/json");
		conn.setConnectTimeout(5000);
		OutputStream outputStream = conn.getOutputStream();
		outputStream.write(parameter.getBytes());
		outputStream.flush();
		try {
			if (conn.getResponseCode() != 200) {
				return null;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

		BufferedReader responseBuffer = new BufferedReader(
				new InputStreamReader((conn.getInputStream())));
		String line = null;
		StringBuffer sb = new StringBuffer();
		while ((line = responseBuffer.readLine()) != null) {
			sb.append(line).append("\r\n");
		}
		if (responseBuffer != null) {
			responseBuffer.close();
		}
		if (conn != null) {
			conn.disconnect();
		}
		return sb.toString();
	}

	/**
	 * 
	 * @Title: getWebContent
	 * @Description: 
	 * @param @param urlString
	 * @param @param charset
	 * @param @param timeout
	 * @param @return
	 * @param @throws IOException 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public static String getWebContent(String urlString, final String charset,
			int timeout) throws IOException {
		if (urlString == null || urlString.length() == 0) {
			return null;
		}
		urlString = (urlString.startsWith("http://") || urlString
				.startsWith("https://")) ? urlString : ("http://" + urlString)
				.intern();
		URL url = new URL(urlString);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestProperty(
				"User-Agent",
				"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
		conn.setRequestProperty("Accept", "text/html");
		conn.setConnectTimeout(5000);
		conn.setDoOutput(true);
		conn.setRequestMethod("GET");
		conn.setRequestProperty("Accept-Charset", charset);
		conn.setRequestProperty("contentType", charset);
		try {
			if (conn.getResponseCode() != 200) {
				return null;
			}
		} catch (IOException e) {
			e.printStackTrace();
			System.out.print(e.getMessage());
			return null;
		}
		InputStream input = conn.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(input,
				charset));
		String line = null;
		StringBuffer sb = new StringBuffer();
		while ((line = reader.readLine()) != null) {
			sb.append(line).append("\r\n");
		}
		if (reader != null) {
			reader.close();
		}
		if (conn != null) {
			conn.disconnect();
		}
		return sb.toString();
	}

	/**
	 * 
	 * @Title: getWebContent
	 * @Description: 获取数据
	 * @param @param urlString
	 * @param @return
	 * @param @throws IOException 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public static String getWebContent(String urlString) throws IOException {
		return getWebContent(urlString, "UTF-8", 5000);
	}

	/**
	 * 
	 * @Title: checkHttpState
	 * @Description: 校验http连接是否有效
	 * @param @param ulr
	 * @param @return 设定文件
	 * @return boolean 返回类型
	 * @throws
	 */
	public static boolean checkHttpState(String urlString) {
		boolean flag = true;
		if (urlString == null || urlString.length() == 0) {
			flag = false;
			return flag;
		}
		try {
			urlString = (urlString.startsWith("http://") || urlString
					.startsWith("https://")) ? urlString
					: ("http://" + urlString).intern();
			URL url = new URL(urlString);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestProperty(
					"User-Agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
			conn.setRequestProperty("Accept", "text/html");
			conn.setConnectTimeout(5000);
			conn.setDoOutput(true);
			conn.setRequestMethod("GET");
			conn.setRequestProperty("Accept-Charset", "utf-8");
			conn.setRequestProperty("contentType", "utf-8");
			if (conn.getResponseCode() != 200) {
				flag = false;
				return flag;
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.print(e.getMessage());
			flag = false;
			return flag;
		}

		return flag;

	}

	public static void main(String[] args) throws IOException {

		String url = ConfigUtil.get("vd.url");
		System.out
				.println(checkHttpState("http://192.168.102.1232:1111/test"));
		// String data =
		// HttpConnection.getWebContent(HttpConnection.layerInfoUrl(
		// "jtsb_sp_pt2", 0,""));
		// // 解析
		// JSONArray noArr = (JSONArray) JSON.parse(data);
		// for (int i = 0; i < noArr.size(); i++) {
		// JSONObject jobj = (JSONObject) noArr.get(i);
		// String temp = jobj.get("rowList").toString();
		// JSONArray rowList = (JSONArray) JSON.parse(temp);
		// for (int j = 0; j < rowList.size(); j++) {
		// JSONObject cluster = (JSONObject) rowList.get(j);
		// JSONObject temp2 = (JSONObject) cluster.get("values");
		// String BH = (String) temp2.get("OBJECTID");
		// String SHAPE = (String) temp2.get("SHAPE");
		// System.out.println("记录数:" + j + ",编号:" + BH + "," + ",坐标:"
		// + SHAPE);
		// }
		//
		// }
	}
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值