/** * 百度接口
* 通过用户ip获取用户所在地
* @param userIp
* @return
*/
public static String getAddressByBD2(String strIP) {
try {
URL url = new URL("http://opendata.baidu.com/api.php?query=" + strIP+"&co=&resource_id=6006&t=1433920989928&ie=utf8&oe=utf-8&format=json");;
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line = null;
StringBuffer result = new StringBuffer();
while ((line = reader.readLine()) != null) {
result.append(line);
}
reader.close();
JSONObject jsStr = new JSONObject(result.toString());
JSONArray jsData = (JSONArray) jsStr.get("data");
JSONObject data= (JSONObject) jsData.get(0);//位置
return (String) data.get("location");
} catch (IOException e) {
return "读取失败";
}
}
运行效果图:
详情请参考:http://matools.com/ip