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

/**
     * 获取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方法请我的另一篇博文。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁山好汉(Ls_man)

可以帮我买一包辣条吗?谢谢!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值