代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
JS
】
百度坐标转换 HttpURLConnection 连接失败 重试
作者:
倪闯
/ 发布于
2015/4/2
/
758
百度坐标转换 HttpURLConnection 连接失败 重试
package com.jttx.poi.utils; import com.mongodb.BasicDBList; import com.mongodb.DBObject; import com.mongodb.util.JSON; import java.io.BufferedInputStream; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.logging.Logger; /** * Created by louis on 2015/3/24. */ /* 坐标转换 详细文档地址:http://developer.baidu.com/map/index.php?title=webapi/guide/changeposition */ public class CoordinateConvert { private static final String ak = "kEFeESQOTRx5CR6PYGPqkPnh"; private static Logger logger = Logger.getLogger(CoordinateConvert.class.getName()); public static String getContent(URL url, int retryNum) { HttpURLConnection connection = null; BufferedInputStream bin = null; boolean connected = false; StringBuilder sb = new StringBuilder(); outer: for (int retry = 0; retry <= retryNum && !connected; retry++) { if (retry > 0) { logger.warning("retry: " + retry + "/" + retryNum); } try { connection = (HttpURLConnection) url.openConnection(); switch (connection.getResponseCode()) { case HttpURLConnection.HTTP_OK: connected = true; break;//fine,go on case HttpURLConnection.HTTP_UNAVAILABLE: logger.warning("retry:unavailable 503"); break; case HttpURLConnection.HTTP_GATEWAY_TIMEOUT: logger.warning("retry:gateway timeout. 504"); break; case HttpURLConnection.HTTP_CLIENT_TIMEOUT: logger.warning("retry:client timeout 408"); break; case HttpURLConnection.HTTP_INTERNAL_ERROR: logger.warning("retry:server error 500"); break; case HttpURLConnection.HTTP_BAD_GATEWAY: logger.warning("retry:bad gateway 502"); break; default: logger.warning(" **unknown response code**."); break outer;//abort } bin = new BufferedInputStream(connection.getInputStream()); int n = -1; byte[] buffer = new byte[2048]; while ((n = bin.read(buffer, 0, buffer.length)) != -1) { String line = new String(buffer, 0, n, "UTF-8"); sb.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) connection.disconnect(); if (bin != null) { try { bin.close(); } catch (IOException e) { e.printStackTrace(); } } } } return sb.toString().length() > 0 ? sb.toString() : null; } /** * From * 1:GPS设备获取的角度坐标; * 2:GPS获取的米制坐标、sogou地图所用坐标; * 3:google地图、soso地图、aliyun地图、mapabc地图和amap地图所用坐标 * 4:3中列表地图坐标对应的米制坐标 * 5:百度地图采用的经纬度坐标 * 6:百度地图采用的米制坐标 * 7:mapbar地图坐标; * 8:51地图坐标 * <p/> * To * 5:bd09ll(百度经纬度坐标), * 6:bd09mc(百度米制经纬度坐标); */ public static BasicDBList convert(String coords, int from, int to, int retry) { BasicDBList list = null; String urlStr = String.format("http://api.map.baidu.com/geoconv/v1/?coords=%s&from=%d&%d=5&ak=%s", coords, from, to, ak); try { URL url = new URL(urlStr); String content = getContent(url, retry); DBObject dbObject = (DBObject) JSON.parse(content); int status = (int) dbObject.get("status"); if (status != 0) { return null; } else { list = (BasicDBList) dbObject.get("result"); } } catch (MalformedURLException e) { e.printStackTrace(); } return list; } public static void main(String[] args) { BasicDBList list = convert("121.6635041,31.13573265;121.6708963,31.13499003", 1, 5, 3); System.out.println(list); } }
试试其它关键字
百度坐标
同语言下
.
Jquery搜索框获取回车事件
.
H5页面添加倒计时,然后自动跳转
.
通过user-agent判断h5页面是在哪个手机App(QQ、微信
.
nginx 禁止未绑定的域名访问
.
JavaScript 获取按键,并屏蔽系统 Window 事件
.
H5之只允许微信浏览器打开,禁止从外部浏览器访问
.
微信打开网址添加在浏览器中打开提示的办法
.
实现JS复制、粘贴,Chrome/Firefox下可用
.
video视频播放,play()、pause()、duration时长、onen
.
HTML5实现MP3上传前的预览和播放时长的获取
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
倪闯
贡献的其它代码
(
4
)
.
select下拉列表的样式
.
百度坐标转换 HttpURLConnection 连接失败 重试
.
番茄时钟
.
创建唯一实例
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3