先获取百度地图的ak,创建应用!!!
https://lbsyun.baidu.com/apiconsole/key#/home
public static void main(String[] args) throws IOException { HttpClient client = HttpClients.createDefault();// 创建默认http连接 HttpPost post = new HttpPost("http://api.map.baidu.com/location/ip");// 创建一个post请求 List<NameValuePair> paramList = new ArrayList<NameValuePair>(); paramList.add(new BasicNameValuePair("ip", "你的ip"));//传递的参数 paramList.add(new BasicNameValuePair("ak", "你的ak"));//传递的参数 //paramList.add(new BasicNameValuePair("sn", "2b91b2409107"));//传递的参数 //paramList.add(new BasicNameValuePair("coor", ""));//传递的参数 // 把参转码后放入请求实体中 HttpEntity entitya = new UrlEncodedFormEntity(paramList, "utf-8"); post.setEntity(entitya);// 把请求实体放post请求中 HttpResponse response = client.execute(post);// 用http连接去执行get请求并且获得http响应 HttpEntity entity = response.getEntity();// 从response中取到响实体 String html = EntityUtils.toString(entity);// 把响应实体转成文本 System.out.println(html); String a = "\u5317\u4eac\u5e02"; String b = "\u5317\u4eac"; String c = "\u5317\u4eac\u5e02"; System.out.println(a); System.out.println(b); System.out.println(c); }
测试: