熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

用百度獲取IP地址經緯度

2013-11-23 19:18:43  來源: Java核心技術 

  下面我們舉例說明一下關於利用百度獲取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;

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26637.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.