欢迎来到258分享网,纯净的网络源码分享基地!

258资源分享网

全部作品
全部作品
网站源码
微信源码
素材特效
源码插件
视频教程
建站学院
热门搜索: 织梦  农业种植  农业  安全设置  官方
258资源分享 > 建站学院 > 微信开发 > nyoj_lvy实战开发系列《三》: 获取城市信息

推荐下载

HTML5响应式自适应网咯设计

2020-05-12   浏览:740

高端HTML5响应式企业通用网

2020-05-06   浏览:521

html5响应式外贸网站英文版

2020-05-08   浏览:510

HTML5自适应律师工作室类网

2020-04-04   浏览:504

HTML5影视传媒文化公司类网

2020-05-12   浏览:501

nyoj_lvy实战开发系列《三》: 获取城市信息

发布时间:2020-12-21  

由于微信小程序没有方法可以获得当前用户所在城市的信息,所以需要调用方法来获取城市信息,用了两个方法去发送请求并返回城市信息 
1.

@Controller public class WechatLocationManager { private Logger logger = LoggerFactory.getLogger(WechatLocationManager.class); @RequestMapping(value = "/wechat/getcity", method = RequestMethod.POST) @ResponseBody public String getCity( @RequestBody Map<String, String> location) { String local = location.get("location"); System.out.println(local); String latitude = local.substring(0, local.indexOf(',')); String longitude = local.substring(local.indexOf(',') + 1); logger.debug("纬度:{}", latitude); logger.debug("经度:{}", longitude); String url = "?ak=2IBKO6GVxbYZvaR2mf0GWgZE&output=json&pois=0" + "&location=" + latitude + "," + longitude; HttpURLConnection connection = null; BufferedReader reader = null; try { URL getUrl = new URL(url); connection = (HttpURLConnection) getUrl.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.connect(); reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); StringBuilder builder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { builder.append(line); } if (logger.isDebugEnabled()) logger.debug(builder.toString()); return JSONObject.fromObject(builder.toString()); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { connection.disconnect(); } } return obj; } }

2.第二个方法是根据项目中提供的发送请求的方法去发送并接受返回的信息

private static final String LOCATION_URL = ""; private static final Map<String, String> LOCATION_INPUT = new ConcurrentHashMap<String,String>(){ { put("ak", "2IBKO6GVxbYZvaR2mf0GWgZE"); put("output", "json"); put("pois","0"); } }; @RequestMapping(value = "/wechat/city", method = RequestMethod.POST) @ResponseBody public String getCity(@RequestBody Map<String, String> location) { String local = location.get("location"); System.out.println(local); String latitude = local.substring(0, local.indexOf(',')); String longitude = local.substring(local.indexOf(',') + 1); logger.debug("纬度:{}", latitude); logger.debug("经度:{}", longitude); LOCATION_INPUT.put("location", local); String obj = HttpClientUtils.doGetWithHeader(null, LOCATION_URL, LOCATION_INPUT, null, "utf-8", null); return obj; }

这里记录一下doGet方法去处理请求的

public static String doGet(CloseableHttpClient client, String url, Map<String, String> params, String charset, String userAgent, Map<String, String> heads) { if (StringUtils.isBlank(url)) { return null; } CloseableHttpResponse response = null; try { if (params != null && !params.isEmpty()) { List<NameValuePair> pairs = new ArrayList<>(params.size()); for (Map.Entry<String, String> entry : params.entrySet()) { String value = entry.getValue(); if (value != null) { pairs.add(new BasicNameValuePair(entry.getKey(), value)); } } url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset)); } HttpGet httpGet = new HttpGet(url); if (StringUtils.isNotBlank(userAgent)) { httpGet.addHeader(HTTP.USER_AGENT, userAgent); } if (heads != null && !heads.isEmpty()) { for (Map.Entry<String, String> entry : heads.entrySet()) { String value = entry.getValue(); if (value != null) { httpGet.addHeader(entry.getKey(),value); } } } response = client.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { httpGet.abort(); throw new RuntimeException("HttpClient,error status code :" + statusCode); } HttpEntity entity = response.getEntity(); String result = null; if (entity != null) { result = EntityUtils.toString(entity, charset); } EntityUtils.consume(entity); response.close(); return result; } catch (Exception e) { throw new RuntimeException(e); } finally { if (null != response) { try { response.close(); } catch (Exception ex) { logger.error("close response has error."); } } } }