歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發之Google天氣顯示(綜合例子)

Android開發之Google天氣顯示(綜合例子)

日期:2017/3/1 11:11:57   编辑:Linux編程

Android的google天氣顯示,包括讀取網絡信息,讀取xml,使用網絡信息二級制碼生成文件

1.生成url
2.獲取google返回的網絡信息 new InputSource(aURL.openStream())
3.生成解析xml的處理器
4.解析xml
5.封裝解析後的xml文件
6.根據解析後的數據,生成url,獲取google返回的天氣圖標,賦值給圖片

代碼:

/*獲取用戶輸入的城市名稱*/
String city = ((EditText) findViewById(R.id.input))
.getText().toString();

/*組成URL字符串*/
//中文:http://www.google.com/ig/api?hl=zh-cn&weather=
//英文:http://www.google.com/ig/api?weather=
String queryString = "http://www.google.com/ig/api?weather="
+ city;
/*將可能的空格替換為"%20"*/
URL aURL = new URL(queryString.replace(" ", "%20"));

/* 從SAXParserFactory獲取SAXParser*/
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();

/* 從SAXParser得到XMLReader*/
XMLReader xr = sp.getXMLReader();

/*
* 創建GoogleWeatherHandler,以便解析XML內容
*/
GoogleWeatherHandler gwh = new GoogleWeatherHandler();
xr.setContentHandler(gwh);

/* 解析XML文件內容 */
xr.parse(new InputSource(aURL.openStream()) );


TextView tv1 = (TextView)findViewById(R.id.tem);
tv1.setText("溫度:" + gwh.getCurrentTemp() + "攝氏度");

TextView tv2 = (TextView)findViewById(R.id.weather);
tv2.setText(gwh.getCurrentCondition());

TextView tv3 = (TextView)findViewById(R.id.hum);
tv3.setText(""+ gwh.getCurrentHum() );


URL iconURL = new URL("http://www.google.com"+ gwh.getIconURL());
URLConnection conn = iconURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
//設置icon
ImageView iv = (ImageView)findViewById(R.id.iconOfWeather);
Bitmap bm = null;
bm = BitmapFactory.decodeStream(bis);
iv.setImageBitmap(bm);
bis.close();
is.close();

分析google返回的xml的處理類代碼:
private boolean in_current_conditions = false;
private boolean in_forecast_conditions = false;

private Integer current_temp;
private String current_condition;
private String current_hum;
private String iconURL;


@Override
public void startDocument() throws SAXException {

}

@Override
public void endDocument() throws SAXException {

}

@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
// 'Outer' Tags
if (localName.equals("forecast_information")) {
this.in_forecast_information = true;
} else if (localName.equals("current_conditions")) {
this.in_current_conditions = true;
} else if (localName.equals("forecast_conditions")) {
this.in_forecast_conditions = true;
} else {
String dataAttribute = atts.getValue("data");
// 'Inner' Tags of "<forecast_information>"
if (localName.equals("city")) {
} else if (localName.equals("postal_code")) {
} else if (localName.equals("latitude_e6")) {
/* One could use this to convert city-name to Lat/Long. */
} else if (localName.equals("longitude_e6")) {
/* One could use this to convert city-name to Lat/Long. */
} else if (localName.equals("forecast_date")) {
} else if (localName.equals("current_date_time")) {
} else if (localName.equals("unit_system")) {
if (dataAttribute.equals("SI"))
this.usingSITemperature = true;
}
// SHARED(!) 'Inner' Tags within "<current_conditions>" AND
// "<forecast_conditions>"
else if (localName.equals("day_of_week")) {
if (this.in_current_conditions) {
//可擴展
} else if (this.in_forecast_conditions) {
//可擴展
}
} else if (localName.equals("icon")) {
if (this.in_current_conditions) {
this.setIconURL(dataAttribute);
} else if (this.in_forecast_conditions) {
//可擴展
}
} else if (localName.equals("condition")) {
if (this.in_current_conditions) {
this.setCurrentCondition(dataAttribute);
} else if (this.in_forecast_conditions) {
//可擴展
}
}
// 'Inner' Tags within "<current_conditions>"
else if (localName.equals("temp_f")) {
//this.setCurrentTemp(Integer.parseInt(dataAttribute));
} else if (localName.equals("temp_c")) {
this.setCurrentTemp(Integer.parseInt(dataAttribute));
} else if (localName.equals("humidity")) {
this.setCurrentHum(dataAttribute);
} else if (localName.equals("wind_condition")) {
//可擴展
}
// 'Inner' Tags within "<forecast_conditions>"
else if (localName.equals("low")) {
int temp = Integer.parseInt(dataAttribute);
if (this.usingSITemperature) {
//可擴展
} else {
//可擴展
}
} else if (localName.equals("high")) {
//int temp = Integer.parseInt(dataAttribute);
if (this.usingSITemperature) {
//可擴展
} else {
//可擴展
}
}
}
}

@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals("forecast_information")) {
this.in_forecast_information = false;
} else if (localName.equals("current_conditions")) {
this.in_current_conditions = false;
} else if (localName.equals("forecast_conditions")) {
this.in_forecast_conditions = false;
}
}

@Override
public void characters(char ch[], int start, int length) {
/*
* 可擴展 <element>characters</element>
*/
}

Copyright © Linux教程網 All Rights Reserved