标签:des address nap todo cache 调用 详解 esc mil
分为两个部分:生成HTML和返回HTML
生成HTML:
最终想要的时显示地图,不可避免的使用高德地图的API。
改变几个参数即可达到目的,很简单不讲了。
重点说说如何生成查询结果对应的HTML:
将HTML中的内容保存为String,在String格式的基础上替换关键参数,即可批量生产。
在将HTML保存为String时,因为HTML内容都是带换行缩进的,还带有许多双引号,所以直接粘贴实不可取的。
我用的方法是将内容先保存在文本中,再通过IO流读取,并且一定要做到:
删除所有换行符
将双引号替换为单引号
将所有注释都去掉
否则不满足前两条,String格式会报错;
不满足最后一条,输出的时候因为没有换行符,“//”又是行注释,会导致把第一个“//”后的所有内容都认为是注释而无法执行。
代码:getHtml.java
1 package util; 2 3 import java.util.List; 4 import java.util.regex.Pattern; 5 6 public class getHtml { 7 public getHtml(){} 8 public String creatHtml(List<String> list){ 9 String html = null; 10 String add = ""; 11 int j = list.size(); 12 for(int i = 1; i < j ; i++ ){ 13 if(i != j - 1){ 14 add = add + list.get(i) + "--->"; 15 }else{ 16 add = add + list.get(i);} 17 } 18 String address = list.get(j-1); 19 html = "<html> <head> <base href=‘<%=basePath%>‘> <meta http-equiv=‘pragma‘ content=‘no-cache‘> <meta http-equiv=‘cache-control‘ content=‘no-cache‘> <meta http-equiv=‘expires‘ content=‘0‘> <meta http-equiv=‘keywords‘ content=‘keyword1,keyword2,keyword3‘> <meta http-equiv=‘description‘ content=‘This is my page‘> <script type=‘text/javascript‘ src=‘http://webapi.amap.com/maps?v=1.3&key=0250860ccb5953fa5d655e8acf40ebb7&plugin=AMap.Geocoder‘></script> <script type=‘text/javascript‘ src=‘http://cache.amap.com/lbs/static/addToolbar.js‘></script> <style> #addressBox{height:20px;width:600px;} #mapBox{height:400px;width:600px} #pointBox{height:20px;width:600px;} </style> <!-- <link rel=‘stylesheet‘ type=‘text/css‘ href=‘styles.css‘> --> </head> <body onload=‘geocoder();‘> " 20 + " <p>物流轨迹:</p> " 21 + "<p>"+add+"</p>" 22 + "<div> <p>当前所在位置</p> </div> <div id=‘pointBox‘> </div> <div id=‘mapBox‘></div> <div> 如果不够准确,可以拖动地图改变经纬度 </div> " 23 + "<script type=‘text/javascript‘> var address =‘"+address 24 + "‘;var $pointBox = document.getElementById(‘pointBox‘); var map = new AMap.Map(‘mapBox‘, { resizeEnable: true, center: [116.397428, 39.90923], zoom:14 }); function addMarker(point) { var marker = new AMap.Marker({ map: map, position: [ point.getLng(), point.getLat()] }); } function addCenterPoint(){ map.clearMap(); var centerPoint = map.getCenter(); addMarker(centerPoint); $pointBox.innerHTML = ‘当前经纬度为:‘ + centerPoint.getLng() + ‘,‘ + centerPoint.getLat(); } addCenterPoint(); function geocoder() { map.clearMap(); var myGeo = new AMap.Geocoder(); myGeo.getLocation(address, function(status, result) { if (status === ‘complete‘ && result.info === ‘OK‘) { geocoder_CallBack(result); }else{ $pointBox.innerHTML = ‘查无此地址‘; } }); } function geocoder_CallBack(data) { var resultStr = ‘‘; var geocode = data.geocodes; addMarker(geocode[0].location); resultStr += ‘当前坐标</b>:‘ + geocode[0].location.getLng() + ‘, ‘ + geocode[0].location.getLat(); map.setFitView(); $pointBox.innerHTML = resultStr; } map.on(‘moveend‘, function() { addCenterPoint(); }); </script> </body></html>"; 25 String str = html.replaceAll( "\‘", "\""); 26 return str; 27 } 28 }
大家可以看到String html定义哪一行是如何处理更改参数和处理格式的。可以和最开始链接中的代码进行对比。
返回HTML:
不说了,直接代码:getMap.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String shipperCode = request.getParameter("ShipperCode"); String logisticCode = request.getParameter("LogisticCode"); System.out.println(shipperCode+","+logisticCode); kdniaoTrackQueryAPI kdnAPI = new kdniaoTrackQueryAPI(); List<String> result = kdnAPI.Return(shipperCode,logisticCode); String name = ""; if(result.get(0).equals("0")){ name = "<html><head><title>t.html</title></head><body>"+result.get(1)+"<br></body></html>"; }else if(result.get(0).equals("1")){ getHtml gH = new getHtml(); name = gH.creatHtml(result); }else{ name = "<html><head><title>t.html</title></head><body>"+result.get(1)+"<br></body></html>"; } //返回文本数据 response.setContentType("text/plain;charset=UTF-8"); ServletOutputStream outputStream = response.getOutputStream(); outputStream.write(name.getBytes("UTF-8")); outputStream.flush(); outputStream.close(); }
随便查查就有返回的方法
调用方法实例:
如何调用我的API
1 package test; 2 import java.io.BufferedReader; 3 import java.io.File; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.io.OutputStreamWriter; 8 import java.io.PrintWriter; 9 import java.net.HttpURLConnection; 10 import java.net.URL; 11 import java.net.URLEncoder; 12 import java.util.Map; 13 14 public class test1 { 15 public static void main(String args[]){ 16 //请求url 17 String URL="安全起见,不可说,等我发布前端地址"; 18 String ShipperCode = ""//物理公司编号; 19 String LogisticCode = ""//物流运单号; 20 String ReqURL = URL + "?ShipperCode=" + ShipperCode + "&LogisticCode=" + LogisticCode; 21 String result = sendPost(ReqURL); 22 write(result,"test"); 23 } 24 /** 25 * 向指定 URL 发送POST方法的请求 26 * @return 远程资源的响应结果 27 */ 28 private static String sendPost(String url) { 29 OutputStreamWriter out = null; 30 BufferedReader in = null; 31 StringBuilder result = new StringBuilder(); 32 try { 33 URL realUrl = new URL(url); 34 HttpURLConnection conn =(HttpURLConnection) realUrl.openConnection(); 35 // 发送POST请求必须设置如下两行 36 conn.setDoOutput(true); 37 conn.setDoInput(true); 38 // POST方法 39 conn.setRequestMethod("POST"); 40 // 设置通用的请求属性 41 conn.setRequestProperty("accept", "*/*"); 42 conn.setRequestProperty("connection", "Keep-Alive"); 43 conn.setRequestProperty("user-agent", 44 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); 45 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 46 conn.setRequestProperty("kdniao-nocache", "true"); 47 conn.connect(); 48 // 获取URLConnection对象对应的输出流 49 out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); 50 // 定义BufferedReader输入流来读取URL的响应 51 in = new BufferedReader( 52 new InputStreamReader(conn.getInputStream(), "UTF-8")); 53 String line; 54 while ((line = in.readLine()) != null) { 55 result.append(line); 56 } 57 } catch (Exception e) { 58 e.printStackTrace(); 59 } 60 //使用finally块来关闭输出流、输入流 61 finally{ 62 try{ 63 if(out!=null){ 64 out.close(); 65 } 66 if(in!=null){ 67 in.close(); 68 } 69 } 70 catch(IOException ex){ 71 ex.printStackTrace(); 72 } 73 } 74 return result.toString(); 75 } 76 /*@result 服务器返回的html 77 *@fileName 保存的文件名字 78 * */ 79 public static void write(String result,String fileName) { 80 FileWriter fw = null; 81 try { 82 //如果文件存在,则追加内容;如果文件不存在,则创建文件 83 File f=new File("E:\\"+fileName+".html"); 84 fw = new FileWriter(f, true); 85 } catch (IOException e) { 86 e.printStackTrace(); 87 } 88 PrintWriter pw = new PrintWriter(fw); 89 pw.print(result); 90 pw.flush(); 91 try { 92 fw.flush(); 93 pw.close(); 94 fw.close(); 95 } catch (IOException e) { 96 e.printStackTrace(); 97 } 98 } 99 }
标签:des address nap todo cache 调用 详解 esc mil
原文地址:http://www.cnblogs.com/cxy2016/p/6884432.html