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

转自 https://blog.csdn.net/ls_man/article/details/78133981

 
 
  1. /**
  2. * 获取ip信息
  3. *
  4. * @param ip
  5. * @return
  6. */
  7. public static String getIpInfo(String ip) {
  8. String httpUrl = "http://ip.taobao.com/service/getIpInfo.php";
  9. String httpArg = "ip=" + ip;
  10. String jsonResult = httpRequest(httpUrl, httpArg);
  11. String ipInfo = "请搜索IP查询";
  12. if (jsonResult != null) {
  13. /*
  14. * {"code":0,"data":{"ip":"210.75.225.254","country":"\u4e2d\u56fd","area":"\u534e\u5317",
  15. "region":"\u5317\u4eac\u5e02","city":"\u5317\u4eac\u5e02","county":"","isp":"\u7535\u4fe1",
  16. "country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",
  17. "county_id":"-1","isp_id":"100017"}}
  18. * */
  19. try {
  20. JSONObject jsonObject = JSONObject.fromObject(jsonResult);
  21. if (jsonObject.get( "code").toString().equals( "0")) {
  22. JSONObject data = jsonObject.getJSONObject( "data");
  23. // 国家/地区
  24. String country = data.getString( "country");
  25. // 区域,如华东
  26. String area = data.getString( "area");
  27. // 省份
  28. String region = data.getString( "region");
  29. // 城市
  30. String city = data.getString( "city");
  31. // 县
  32. String county = data.getString( "county");
  33. // 运营商
  34. String isp = data.getString( "isp");
  35. ipInfo = region + city + county + isp;
  36. }
  37. } catch (Exception e) {
  38. //解析失败
  39. }
  40. }
  41. return ipInfo;
  42. }


其中调用了httpRequest方法请我的另一篇博文。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值