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


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

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

    2 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>
    
    

    3 参照GPS坐标偏移接口文档,具体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);
     }
    
    }
    
    

    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"}]} 就对了。

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

  • 相关阅读:
    PostgreSQL初识,编译安装
    关于iframe的父页面调取子页面里的事件(父往子里传)
    织梦常用代码
    获取月份的最后一天是几号
    获取今天、昨天、一周前的日期
    时分秒倒计时的js实现
    CSS初始化代码
    正则表达式
    utils.js文件;一些常用方法的备份
    js replace替换一段文本中所有的相同字符
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3111448.html
Copyright © 2020-2023  润新知