获取天气的链接地址
根据经纬度获取:http://www.google.com/ig/api?weather=,,,31174165,121433841
【如中山的经纬度是:22.516997123628076,113.39263916015625 必须都乘以1000000才能作为参数】
int a=(int)22.516997123628076*1000000;int b=(int)113.39263916015625*1000000;String strUrl ="http://www.google.com/ig/api?hl=zh-cn&weather=,,,"+a+","+b;System.out.println(strUrl);根据地名获取:http://www.google.com/ig/api?hl=zh-cn&weather=Beijing
String strData = ""; String strUrl = "http://www.google.com/ig/api?hl=zh-cn&weather=ZhongShan"; strData = getResponse(strUrl); // 天气预报的xml存储在sd卡中 new FileUnit().write(strData, "weather.xml");// SAX解析xml try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); SAXReader saxReader = new SAXReader(); InputSource is = new InputSource(); is.setByteStream(new ByteArrayInputStream(strData.getBytes())); sp.parse(is, saxReader); weatherList=saxReader.getWeathList(); } catch (Exception e) { e.printStackTrace(); }//显示天气预报 showWeather();根据地址 获得xml的String
protected String getResponse(String queryURL) { URL url;try { url = new URL(queryURL.replace(" ", " ")); URLConnection urlconn = url.openConnection(); urlconn.connect(); InputStream is = urlconn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer buf = new ByteArrayBuffer(50);int read_data = -1;while ((read_data = bis.read()) != -1) { buf.append(read_data); }// String resp = buf.toString(); String resp = EncodingUtils.getString(buf.toByteArray(), "GBK");return resp; } catch (MalformedURLException e) {// TODO Auto-generated catch block e.printStackTrace();return ""; } catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();return ""; } }显示天气
private void showWeather(){ List<String> weather=null; String strTemp="";for(int i=0; i<weatherList.size();i++) { weather=weatherList.get(i); strTemp+="\n==========================\n";for(int j=0;j<weather.size();j++) { strTemp+=weather.get(j)+"\n"; } } tvShow.setText(strTemp); }SAXReader:
View Code package com.ReadOrder;import java.util.ArrayList;import java.util.List;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;public class SAXReader extends DefaultHandler { private final String FORECASE_INFORMATION= "forecast_information";private final String CURRENT_CONDITIONS="current_conditions";private final String FORECAST_CONDITIONS="forecast_conditions";private List<List<String>> weatherList = null;private List<String> weather = null;private static String tagname = ""; @Overridepublic void startDocument() throws SAXException { weatherList = new ArrayList<List<String>>(); } @Overridepublic void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {if (localName.equals(FORECASE_INFORMATION)||localName.equals(CURRENT_CONDITIONS)||localName.equals(FORECAST_CONDITIONS)) { tagname = "current_conditions"; weather = new ArrayList<String>(); } else { if (tagname.equals(CURRENT_CONDITIONS)&& attributes.getValue("data") != null) { weather.add (attributes.getValue("data")); System.out.println("###" + attributes.getValue("data")); } } } @Overridepublic void endElement(String uri, String localName, String qName)throws SAXException { if (localName.equals(FORECASE_INFORMATION)||localName.equals(CURRENT_CONDITIONS)||localName.equals(FORECAST_CONDITIONS)) { weatherList.add(weather); weather=null; tagname=""; } } @Overridepublic void endDocument() throws SAXException { }public List<List<String>> getWeathList() { if(weatherList==null||weatherList.size()<1) {return null; }else { for(int i=0;i<weatherList.size();i++) { System.out.println(weatherList.get(i)); } return weatherList; } }}详细代码:
View Code package com.ReadOrder;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import javax.crypto.spec.IvParameterSpec;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.apache.http.util.ByteArrayBuffer;import org.apache.http.util.EncodingUtils;import org.w3c.dom.ls.LSException;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import android.R.integer;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;public class GoogleWeatherActivity extends Activity { TextView tvShow; List<List<String>> weatherList=null; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setTitle("获取天气预报"); setContentView(R.layout.layout_weather); tvShow = (TextView) findViewById(R.id.TextView001); tvShow.setText("None Data"); findViewById(R.id.btnGetWeather).setOnClickListener(new OnClickListener() { @Overridepublic void onClick(View arg0) { System.out.println("btnGetWeather===>onclick"); String strData = ""; String strUrl = "http://www.google.com/ig/api?hl=zh-cn&weather=ZhongShan"; strData = getResponse(strUrl); // 天气预报的xml存储在sd卡中 new FileUnit().write(strData, "weather.xml");// SAX解析xml try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); SAXReader saxReader = new SAXReader(); InputSource is = new InputSource(); is.setByteStream(new ByteArrayInputStream(strData.getBytes())); sp.parse(is, saxReader); weatherList=saxReader.getWeathList(); } catch (Exception e) { e.printStackTrace(); }//显示天气预报 showWeather(); } }); }private void showWeather(){ List<String> weather=null; String strTemp="";for(int i=0; i<weatherList.size();i++) { weather=weatherList.get(i); strTemp+="\n==========================\n";for(int j=0;j<weather.size();j++) { strTemp+=weather.get(j)+"\n"; } } tvShow.setText(strTemp); }protected String getResponse(String queryURL) { URL url;try { url = new URL(queryURL.replace(" ", " ")); URLConnection urlconn = url.openConnection(); urlconn.connect(); InputStream is = urlconn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer buf = new ByteArrayBuffer(50);int read_data = -1;while ((read_data = bis.read()) != -1) { buf.append(read_data); }// String resp = buf.toString(); String resp = EncodingUtils.getString(buf.toByteArray(), "GBK");return resp; } catch (MalformedURLException e) {// TODO Auto-generated catch block e.printStackTrace();return ""; } catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();return ""; } }}转载于:https://www.cnblogs.com/xiaobuild/archive/2011/08/31/2160809.html