android 得到本地天气情况

今天项目新添加了得到本地当天的温度,湿度,pm2.5的值的需求,研究了下,记下劳动成果,为码农少走弯路做贡献。

思路如下:

1.得到手机的外网ip(http://ip-api.com/json)

2.得到本地信息:省份和城市名(http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=)

3.根据省份城市名拿到城市代码(https://code.csdn.net/snippets/621277/master/blog_20150317_1_5052706/raw)

4.根据城市代码得到天气情况 (http://wthrcdn.etouch.cn/WeatherApi?citykey=)


当然天气情况里面有很多这里只拿温度,湿度和pm2.5

具体代码如下:


 

package com.smart.model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class WeatherCondition {

	 public  static String getIpUrl = "http://ip-api.com/json";
	 public  String getAddress = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=";
	 public  String cityCode = "https://code.csdn.net/snippets/621277/master/blog_20150317_1_5052706/raw";
	 public  String weatherUrl = "http://wthrcdn.etouch.cn/WeatherApi?citykey=";
//	 http://m.weather.com.cn/mweather/101280601.shtml
	 private  String locaInfo;
	 public  SearchWeatherListner weatherListner;
	 private String city ;
	 
	 public interface SearchWeatherListner{
		 
		 public void weatherInfo(String city,String tem,String shidu,String pm);
		 public void error(Exception e);
		 
	 }
	 
	 

	    public WeatherCondition(SearchWeatherListner weatherListner) {
		super();
		this.weatherListner = weatherListner;
	}


		public void getWebIp(final String urlStr) {
	        new Thread() {
	            public void run() {
	                String strForeignIP = "";
	                try {
	                    URL url = new URL(urlStr);

	                    BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));

	                    String s = "";
	                    StringBuffer sb = new StringBuffer("");
	                    while ((s = br.readLine()) != null) {
	                        sb.append(s + "\r\n");
	                    }
	                    br.close();

	                    String webContent = "";
	                    webContent = sb.toString();
	                    System.out.println("webContent:"+webContent);
	                    if(urlStr.equals(getIpUrl)){//1 得到ip
	                    	getIp(webContent);
	                    }else if(urlStr.equals(cityCode)){//3 得到所有cityCode
	                    	getAddress(locaInfo,new JSONObject(webContent));
	                    }
	                    else if(urlStr.indexOf(weatherUrl)!=-1){//4 得到天气情况
	                    	 webContent = sb.toString();
	                    	 parserXml(webContent);
	                    }
	                    else{//2 得到本地信息
	                    	locaInfo =webContent;
	                    	getWebIp(cityCode);
	                    }
	                } catch (Exception e) {
	                	weatherListner.error(e);
	                    e.printStackTrace();
	                }
	            };
	        }.start();

	    }
		public void parserXml(String str){
			
			String wendu = str.substring(str.indexOf("<wendu>"), str.indexOf("</wendu>")).replace("<wendu>", "");
			String shidu = str.substring(str.indexOf("<shidu>"), str.indexOf("</shidu>")).replace("<shidu>", "");
			String pm25 = str.substring(str.indexOf("<pm25>"), str.indexOf("</pm25>")).replace("<pm25>", "");
			System.out.println("wendu:"+wendu+" shidu:"+shidu+" pm25:"+pm25);
	        weatherListner.weatherInfo(city,wendu, shidu, pm25);
		}
		
	    
	    
	    public void getAddress(String str,JSONObject cityJson){
	    	try {
				JSONObject json = new JSONObject(str);
				String country = json.getString("country");
				String province = json.getString("province");
				city = json.getString("city");
				JSONArray ja_province = cityJson.getJSONArray("城市代码");
				for(int i=0;i<ja_province.length();i++){
					JSONObject jo_province =  ja_province.getJSONObject(i);
					if(jo_province.getString("省").equals(province)){
						JSONArray ja_city = jo_province.getJSONArray("市");
						for(int j=0;j<ja_city.length();j++){
							JSONObject jo_city = ja_city.getJSONObject(j);
							if(jo_city.getString("市名").equals(city)){
								String code = jo_city.getString("编码");
								getWebIp(weatherUrl+code);
								System.out.println("code:"+code);
								break;
							}
							
						}
						
						break;
					}
					
				}
				
				
			} catch (JSONException e) {
				weatherListner.error(e);
				e.printStackTrace();
			}
	    }
	    
	    public void getIp(String str){
	    	try {
				JSONObject json = new JSONObject(str);
				String ip = json.getString("query");
				getWebIp(getAddress+ip);
			} catch (JSONException e) {
				weatherListner.error(e);
				e.printStackTrace();
			}
	    	
	    	
	    }
	    
	    
	    
}

代码调用该类 new WeatherCondition(this).getWebIp(WeatherCondition.getIpUrl);

并监听WeatherCondition.SearchWeatherListner改接口

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值