比较简单的根据经纬度解析google天气

来源:互联网 发布:专利运营企业知乎 编辑:程序博客网 时间:2024/05/22 02:13
package com.okhiking;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.FactoryConfigurationError;import javax.xml.parsers.ParserConfigurationException;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpUriRequest;import org.apache.http.impl.client.DefaultHttpClient;import org.w3c.dom.Document;import org.w3c.dom.NodeList;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import android.util.Log;public class GoogleWeather {public String getWeatherData(Integer latitude, Integer longtitude)throws ClientProtocolException, IOException,ParserConfigurationException, FactoryConfigurationError,SAXException {String wheatherResult = null;// String// url="http://www.google.com/ig/api?hl=zh_cn&weather=,,,"+latitude+","+longtitude;String url = "http://www.google.com/ig/api?weather=,,," + latitude+ "," + longtitude;DefaultHttpClient client = new DefaultHttpClient();HttpUriRequest Request = new HttpGet(url);HttpResponse Response = client.execute(Request);HttpEntity Entity = Response.getEntity();InputStream stream = Entity.getContent();InputStreamReader in = new InputStreamReader(stream, "GBK");DocumentBuilder Builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();Document doc = Builder.parse(new InputSource(in));NodeList n = doc.getElementsByTagName("current_conditions");Log.d("log", "Node Length=" + n.getLength());for (int i = 0; i < n.getLength(); i++)// 遍列current_condition所有节点// 比较简单的解析xml的方法{// 获取节点的天气数据String condition = n.item(i).getChildNodes().item(0).getAttributes().item(0).getNodeValue();String tempF = n.item(i).getChildNodes().item(1).getAttributes().item(0).getNodeValue();String tempC = n.item(i).getChildNodes().item(2).getAttributes().item(0).getNodeValue();String humidity = n.item(i).getChildNodes().item(3).getAttributes().item(0).getNodeValue();String wind_condition = n.item(i).getChildNodes().item(5).getAttributes().item(0).getNodeValue();wheatherResult = "Condition:" + condition + "\n" + "TEM:" + tempF + " F\n"+ "TEM:" + tempC + "℃\n" + humidity + "\n" + wind_condition;}// NodeList nforecast_conditions = doc// .getElementsByTagName("forecast_conditions");//// Log.d("log", "Node2 Length=" + nforecast_conditions.getLength());//// for (int i = 0; i < nforecast_conditions.getLength(); i++)//// 遍列current_condition所有节点// {// // 获取节点的天气数据// String week = nforecast_conditions.item(i).getChildNodes().item(0)// .getAttributes().item(0).getNodeValue();// String low = nforecast_conditions.item(i).getChildNodes().item(1)// .getAttributes().item(0).getNodeValue();// String high = nforecast_conditions.item(i).getChildNodes().item(2)// .getAttributes().item(0).getNodeValue();// wheatherResult = wheatherResult + "condition:" + week + "\n" + low// + " 摄氏度\n" + high + "摄氏度\n";// }// Log.d("log", "sTemp=" + wheatherResult);return wheatherResult;}}

原创粉丝点击