Java查询IP通过IP获取地理位置和天气状况工具类


import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author allen
 */
public class IPInfoUtil {

    private static final Logger log = LoggerFactory.getLogger(IPInfoUtil.class);
    /**
     * Mob全国天气预报接口
     */
    private final static String GET_WEATHER="http://apicloud.mob.com/v1/weather/ip?key=270c4d225bcf0&ip=";

    /**
     * 获取客户端IP地址
     * @param request 请求
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if (ip.equals("127.0.0.1")) {
                //根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ip = inet.getHostAddress();
            }
        }
        // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
        if (ip != null && ip.length() > 15) {
            if (ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }
        return ip;
    }

    /**
     * 获取IP返回地理天气信息
     * @param ip ip地址
     * @return
     */
    public static String getIpInfo(String ip){
        if(null != ip){
            String url = GET_WEATHER + ip;
            String result=HttpUtil.sendGet(url);
            return result;
        }
        return null;
    }

    /**
     * 获取IP返回地理信息
     * @param ip ip地址
     * @return
     */
    public static String getIpCity(String ip){
        if(null != ip){
            String url = GET_WEATHER + ip;
            String json=HttpUtil.sendGet(url);
            String result="未知";
            try{
                IpWeatherResult weather=new Gson().fromJson(json,IpWeatherResult.class);
                if (weather.getResult() == null) return result;
                result=weather.getResult().get(0).getCity()+" "+weather.getResult().get(0).getDistrct();
            }catch (Exception e){
                e.printStackTrace();
            }
            return result;
        }
        return null;
    }

    public static void main(String[] args){
        System.out.println(getIpInfo("171.88.85.176"));
    }
}

pojo


import java.io.Serializable;
import java.util.List;

/**
 * @author alln
 */
public class IpWeatherResult implements Serializable {
	private static final long serialVersionUID = 1L;

	String msg;

	List<City> result;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public List<City> getResult() {
		return result;
	}

	public void setResult(List<City> result) {
		this.result = result;
	}
}

获取天气结果

{
	"msg": "success",
	"result": [{
		"airCondition": "良",
		"airQuality": {
			"aqi": 68,
			"city": "成都",
			"district": "成都",
			"fetureData": [{
				"aqi": 75,
				"date": "2019-03-12",
				"quality": "良"
			}, {
				"aqi": 67,
				"date": "2019-03-13",
				"quality": "良"
			}, {
				"aqi": 64,
				"date": "2019-03-14",
				"quality": "良"
			}, {
				"aqi": 70,
				"date": "2019-03-15",
				"quality": "良"
			}, {
				"aqi": 53,
				"date": "2019-03-16",
				"quality": "良"
			}, {
				"aqi": 63,
				"date": "2019-03-17",
				"quality": "良"
			}],
			"hourData": [{
				"aqi": 57,
				"dateTime": "2019-03-11 15:00:00"
			}, {
				"aqi": 68,
				"dateTime": "2019-03-11 14:00:00"
			}, {
				"aqi": 83,
				"dateTime": "2019-03-11 13:00:00"
			}, {
				"aqi": 99,
				"dateTime": "2019-03-11 12:00:00"
			}, {
				"aqi": 107,
				"dateTime": "2019-03-11 11:00:00"
			}, {
				"aqi": 117,
				"dateTime": "2019-03-11 09:00:00"
			}, {
				"aqi": 114,
				"dateTime": "2019-03-11 08:00:00"
			}, {
				"aqi": 113,
				"dateTime": "2019-03-11 07:00:00"
			}, {
				"aqi": 112,
				"dateTime": "2019-03-11 06:00:00"
			}, {
				"aqi": 109,
				"dateTime": "2019-03-11 05:00:00"
			}, {
				"aqi": 109,
				"dateTime": "2019-03-11 04:00:00"
			}, {
				"aqi": 115,
				"dateTime": "2019-03-11 03:00:00"
			}, {
				"aqi": 112,
				"dateTime": "2019-03-11 02:00:00"
			}, {
				"aqi": 108,
				"dateTime": "2019-03-11 01:00:00"
			}, {
				"aqi": 103,
				"dateTime": "2019-03-11 00:00:00"
			}, {
				"aqi": 98,
				"dateTime": "2019-03-10 23:00:00"
			}, {
				"aqi": 93,
				"dateTime": "2019-03-10 22:00:00"
			}, {
				"aqi": 83,
				"dateTime": "2019-03-10 21:00:00"
			}, {
				"aqi": 64,
				"dateTime": "2019-03-10 20:00:00"
			}, {
				"aqi": 55,
				"dateTime": "2019-03-10 19:00:00"
			}, {
				"aqi": 51,
				"dateTime": "2019-03-10 18:00:00"
			}, {
				"aqi": 52,
				"dateTime": "2019-03-10 17:00:00"
			}, {
				"aqi": 57,
				"dateTime": "2019-03-10 16:00:00"
			}],
			"no2": 20,
			"pm10": 54,
			"pm25": 40,
			"province": "四川",
			"quality": "良",
			"so2": 7,
			"updateTime": "2019-03-11 15:00:00"
		},
		"city": "成都",
		"coldIndex": "低发期",
		"date": "2019-03-11",
		"distrct": "成都",
		"dressingIndex": "夹衣类",
		"exerciseIndex": "非常适宜",
		"future": [{
			"date": "2019-03-11",
			"dayTime": "多云",
			"night": "多云",
			"temperature": "19°C / 12°C",
			"week": "今天",
			"wind": "无持续风向 小于3级"
		}, {
			"date": "2019-03-12",
			"dayTime": "小雨",
			"night": "小雨",
			"temperature": "18°C / 12°C",
			"week": "星期二",
			"wind": "无持续风向 小于3级"
		}, {
			"date": "2019-03-13",
			"dayTime": "小雨",
			"night": "阴",
			"temperature": "15°C / 12°C",
			"week": "星期三",
			"wind": "无持续风向 小于3级"
		}, {
			"date": "2019-03-14",
			"dayTime": "多云",
			"night": "小雨",
			"temperature": "18°C / 12°C",
			"week": "星期四",
			"wind": "微风 小于3级"
		}, {
			"date": "2019-03-15",
			"dayTime": "小雨",
			"night": "小雨",
			"temperature": "16°C / 10°C",
			"week": "星期五",
			"wind": "微风 小于3级"
		}, {
			"date": "2019-03-16",
			"dayTime": "小雨",
			"night": "小雨",
			"temperature": "12°C / 10°C",
			"week": "星期六",
			"wind": "微风 小于3级"
		}, {
			"date": "2019-03-17",
			"dayTime": "阴天",
			"night": "阴天",
			"temperature": "18°C / 9°C",
			"week": "星期日",
			"wind": "西风 1级"
		}, {
			"date": "2019-03-18",
			"dayTime": "阴天",
			"night": "阴天",
			"temperature": "19°C / 11°C",
			"week": "星期一",
			"wind": "东北偏东风 2级"
		}, {
			"date": "2019-03-19",
			"dayTime": "多云",
			"night": "阵雨",
			"temperature": "18°C / 11°C",
			"week": "星期二",
			"wind": "东北偏北风 2级"
		}],
		"humidity": "湿度:39%",
		"pollutionIndex": "68",
		"province": "四川",
		"sunrise": "07:52",
		"sunset": "18:43",
		"temperature": "19℃",
		"time": "15:53",
		"updateTime": "20190311161254",
		"washIndex": "不太适宜",
		"weather": "晴",
		"week": "周一",
		"wind": "西南风1级"
	}],
	"retCode": "200"
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:C马雯娟 返回首页
实付 9.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值