`
蓝色的墨
  • 浏览: 76517 次
  • 性别: Icon_minigender_1
  • 来自: 河南
社区版块
存档分类
最新评论

写一个servlet实现gps坐标偏移接口功能

 
阅读更多

             写一个servlet实现GPS坐标偏移接口功能

这两天写了一个servlet,用于实现GPS坐标偏移接口功能。大概就是,我需要首先接收一个经纬度串数组,然后用json解析,给URL服务发送请求,服务器返回json格式的数组.

 

0、需要引入json-simple-1.1的类库。

 

1、GPS坐标偏移接口文档见附件

 

2、具体servlet代码:

 

package com.tm.gpsController;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class GPSConventCoordinateServlet extends HttpServlet {

 private static final long serialVersionUID = 3018725984829044717L;

 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {

  // 我申请的key
  String a_k = "XXXXXX";

  String coors = req.getParameter("lonLatStr");

  JSONParser parser = new JSONParser();
  Object obj = "";
  try {
   obj = parser.parse(coors);
  } catch (ParseException e) {
   e.printStackTrace();
  }
  String lonLatStr = "";
  JSONArray array = (JSONArray) obj;
  for (Object j : array) {
   //
   lonLatStr = lonLatStr + ((JSONObject) j).get("lon") + ","
     + ((JSONObject) j).get("lat") + ";";
  }

  lonLatStr = lonLatStr.substring(0, lonLatStr.length() - 1);
  String postParam = "config=RGC&flag=true&resType=json&cr=0&coors="
    + lonLatStr + "&a_k=" + a_k;
  System.out.println(postParam);
  // 服务地址
  String urlString = "XXXXXX
";

  URL url = new URL(urlString);
  URLConnection urlc = url.openConnection();

  HttpURLConnection httpConnection = (HttpURLConnection) urlc;
  httpConnection.setRequestMethod("POST");
  httpConnection.setDoInput(true);
  httpConnection.setDoOutput(true);

  httpConnection.connect();

  OutputStreamWriter osw = new OutputStreamWriter(
    new BufferedOutputStream(httpConnection.getOutputStream()));
  osw.write(postParam);
  osw.flush();
  System.out.println();
  osw.close();
  BufferedReader br = new BufferedReader(new InputStreamReader(
    httpConnection.getInputStream()));
  String line;
  StringBuffer sbuffB = new StringBuffer();
  while ((line = br.readLine()) != null) {

   sbuffB.append(line + "\n");
   System.out.println(line);
  }
  br.close();
  httpConnection.disconnect();

 }

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  doGet(req, resp);
 }

}

 

3、web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="
http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 
    <welcome-file-list>
         <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
 
 
  <servlet>
    <servlet-name>GPSConventCoordinateServlet</servlet-name>
    <servlet-class>com.tm.gpsController.GPSConventCoordinateServlet</servlet-class>
  </servlet>

 
  <servlet-mapping>
    <servlet-name>GPSConventCoordinateServlet</servlet-name>
    <url-pattern>/GPSConventCoordinateServlet</url-pattern>
  </servlet-mapping>
 
 
</web-app>

 

4、启动tomcat,部署一下,运行。

 

5、在浏览器中输入

http://localhost:8080/gpsCoordinateConvent/GPSConventCoordinateServlet?lonLatStr=[{"lon":133.34,"lat":35.33},{"lon":134.33,"lat":34.33]进行测试。

 

6、在控制台输出返回的信息{"message":"ok","list":[{"gridcode":"873482645301","y":"582.5422219509549","x":"582.5422219509549"},{"gridcode":"873482645301","y":"582.5422219509549","x":"582.5422219509549"}]} 就对了

 

 

7、注意 在浏览器中输入的参数lonLatStr=[{"lon":133.34,"lat":35.33},{"lon":134.33,"lat":34.33] 参数格式有时候输入的特殊,服务器端就会返回一个默认的返回的结果。"[{"gridcode":"873482645301","y":"582.5422219509549","x":"582.5422219509549"},这时候,其实程序是没有问题的,只需要改一下参数,就可以了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics