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

来源:互联网 发布:汇川 4da编程 编辑:程序博客网 时间:2024/05/21 06:26
/**     * 获取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方法请我的另一篇博文。

原创粉丝点击