java IP定位 接入百度地图API

public String ipLocation(String ip) {
        HttpClient client = HttpClients.createDefault();// 创建默认http连接
        HttpPost post = new HttpPost("http://api.map.baidu.com/location/ip");// 创建一个post请求
        List<NameValuePair> paramList = new ArrayList<>();
        paramList.add(new BasicNameValuePair("ip", ip));//传递的参数
        paramList.add(new BasicNameValuePair("ak", "MzZPA1qIFLHoMi8ZbCxi0PTlgbMw8gPo"));//传递的参数
        paramList.add(new BasicNameValuePair("sn", "9b35281dac9f4b96941ce9468cbede2b"));//传递的参数
        paramList.add(new BasicNameValuePair("coor", ""));//传递的参数
        // 把参转码后放入请求实体中
        HttpEntity entitya = null;
        try {
            entitya = new UrlEncodedFormEntity(paramList, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        post.setEntity(entitya);// 把请求实体放post请求中
        HttpResponse response = null;// 用http连接去执行get请求并且获得http响应
        try {
            response = client.execute(post);
        } catch (IOException e) {
            e.printStackTrace();
        }

        HttpEntity entity = response.getEntity();// 从response中取到响实体
        String html = null;// 把响应实体转成文本
        try {
            html = EntityUtils.toString(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }
        IPEntity iPEntity = Static.GSON.fromJson(html, IPEntity.class);

        try {
            return iPEntity.getContent().getAddress();
        } catch (NullPointerException npe) {

        }
//        System.out.println(html);
//
       String a="\u5317\u4eac\u5e02";
       System.out.println(a);
//        return html;
        return null;
    }

    public class IPEntity {
        private String address;
        private IPContentEntity content;

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }


        public IPContentEntity getContent() {
            return content;
        }

        public void setContent(IPContentEntity content) {
            this.content = content;
        }
    }

    public class IPContentEntity {
        private String address;
        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }
    }
分享一个在线转码,IP定位小工具——头条在线工具

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值