大致思路是客户端发起请求,我们首先根据请求获取到外网IP,然后再根据外网IP获取到用户所在城市,最后根据城市获取到天气信息
获取外网IP
万网获取外网IP地址: www.net.cn/static/cust…
/**
* @Description:获取客户端外网ip 此方法要接入互联网才行,内网不行
**/
public static String getPublicIp() {
try {
String path = "http://www.net.cn/static/customercare/yourip.asp";// 要获得html页面内容的地址(万网)
URL url = new URL(path);// 创建url对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 打开连接
conn.setRequestProperty("contentType", "GBK"); // 设置url中文参数编码
conn.setConnectTimeout(5 * 1000);// 请求的时间
conn.setRequestMethod("GET");// 请求方式
InputStream inStream = conn.getInputStream();
// readLesoSysXML(inStream);
BufferedReader in = new BufferedReader(new InputStreamReader(
inStream, "GBK"));
StringBuilder buffer = new StringBuilder();
String line;
// 读取获取到内容的最后一行,写入
while ((line = in.readLine()) != null) {
buffer.append(line