您的位置:首页 > 理论基础 > 计算机网络

Java_调用HttpRequest访问淘宝开发API查询IP信息

2017-09-29 15:06 633 查看
/**
* 获取ip信息
*
* @param ip
* @return
*/
public static String getIpInfo(String ip) {
String httpUrl = "http://ip.taobao.com/service/getIpInfo.php";
String httpArg = "ip=" + ip;
String jsonResult = httpRequest(httpUrl, httpArg);
String ipInfo = "请搜索IP查询";
if (jsonResult != null) {
/*
* {"code":0,"data":{"ip":"210.75.225.254","country":"\u4e2d\u56fd","area":"\u534e\u5317",
"region":"\u5317\u4eac\u5e02","city":"\u5317\u4eac\u5e02","county":"","isp":"\u7535\u4fe1",
"country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",
"county_id":"-1","isp_id":"100017"}}
* */
try {
JSONObject jsonObject = JSONObject.fromObject(jsonResult);
if (jsonObject.get("code").toString().equals("0")) {
JSONObject data = jsonObject.getJSONObject("data");
// 国家/地区
String country = data.getString("country");
// 区域,如华东
String area = data.getString("area");
// 省份
String region = data.getString("region");
// 城市
String city = data.getString("city");
// 县
String county = data.getString("county");
// 运营商
String isp = data.getString("isp");

ipInfo = region + city + county + isp;
}
} catch (Exception e) {
//解析失败
}
}
return ipInfo;
}


其中调用了httpRequest方法请我的另一篇博文。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: