package com.dashan.utils.iputils; import com.fasterxml.jackson.databind.ObjectMapper; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import java.util.Map; /** * 从城市查询 * * @author ADMIN */ public class AddressUtil { public static String getAddresses(String ip) { OkHttpClient httpClient = new OkHttpClient(); String url = "http://whois.pconline.com.cn/ipJson.jsp?ip=" + ip + "&json=true"; Request request = new Request.Builder() .url(url) .build(); try { Response response = httpClient.newCall(request).execute(); String result = response.body().string(); ObjectMapper objectMapper = new ObjectMapper(); Map resultMap = objectMapper.readValue(result, Map.class); System.out.println("ip信息:" + resultMap); return resultMap.get("pro") + (String) resultMap.get("city"); } catch (Exception e) { e.printStackTrace(); } return null; } public static void main(String[] args) { System.out.println(AddressUtil.getAddresses("119.127.170.99")); } }
标签:java,String,ip,公网,resultMap,okhttp3,IP,import,ObjectMapper From: https://www.cnblogs.com/xbinbin/p/17190167.html