电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

用百度获取IP地址经纬度


发布日期:2023/6/17
 

下面我们举例说明一下关于利用百度获取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;

}

上一篇:Groovy轻松入门—搭建Groovy开发环境

下一篇:Notify()和notifyall()的原理