下面我们举例说明一下关于利用百度获取IP地址经纬度的方法: /** * 获取指定IP对应的经纬度(为空返回当前机器经纬度) * * @param ip * @return */ public static String[] getIPXY(String ip) { String ak = 百度申请的Key; if (null == ip) { ip = ; } try { URL url = new URL( + ak + &ip= + ip + &coor=bdll); InputStream inputStream = urlopenStream(); InputStreamReader inputReader = new InputStreamReader(inputStream); BufferedReader reader = new BufferedReader(inputReader); StringBuffer sb = new StringBuffer(); String str; do { str = readerreadLine(); sbappend(str); } while (null != str); str = sbtoString(); if (null == str || strisEmpty()) { return null; } // 获取坐标位子 int index = strindexOf(point); int end = strindexOf(}} index); if (index == || end == ) { return null; } str = strsubstring(index end + ); if (null == str || strisEmpty()) { return null; } String[] ss = strsplit(:); if (sslength != ) { return null; } String x = ss[]split()[]; String y = ss[]; x = xsubstring(xindexOf(\) + xindexOf(\ )); y = xsubstring(yindexOf(\) + yindexOf(\ )); return new String[] { x y }; } catch (MalformedURLException e) { eprintStackTrace(); } catch (IOException e) { eprintStackTrace(); } return null; } |