?? cityweather.java
字號(hào):
package com.yarin.android.CityWeather;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.InputSource;import org.xml.sax.XMLReader;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.Spinner;public class CityWeather extends Activity{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init() { Spinner city_spr = (Spinner) findViewById(R.id.Spinner01); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ConstData.city); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); city_spr.setAdapter(adapter); Button submit = (Button) findViewById(R.id.Button01); submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Spinner spr = (Spinner) findViewById(R.id.Spinner01); Long l = spr.getSelectedItemId(); int index = l.intValue(); String cityParamString = ConstData.cityCode[index]; try { URL url = new URL(ConstData.queryString + cityParamString); getCityWeather(url); } catch (Exception e) { Log.e("CityWeather", e.toString()); } } }); Button submit_input = (Button) findViewById(R.id.Button001); submit_input.setOnClickListener(new OnClickListener() { public void onClick(View v) { EditText inputcity = (EditText) findViewById(R.id.EditText001); String tmp = inputcity.getText().toString(); try { URL url = new URL(ConstData.queryString_intput + tmp); getCityWeather(url); } catch (Exception e) { Log.e("CityWeather", e.toString()); } } }); } // 更新顯示實(shí)時(shí)天氣信息 private void updateWeatherInfoView(int aResourceID, WeatherCurrentCondition aWCC) throws MalformedURLException { URL imgURL = new URL("http://www.google.com/" + aWCC.getIcon()); ((SingleWeatherInfoView) findViewById(aResourceID)).setWeatherIcon(imgURL); ((SingleWeatherInfoView) findViewById(aResourceID)).setWeatherString(aWCC.toString()); } // 更新顯示天氣預(yù)報(bào) private void updateWeatherInfoView(int aResourceID, WeatherForecastCondition aWFC) throws MalformedURLException { URL imgURL = new URL("http://www.google.com/" + aWFC.getIcon()); ((SingleWeatherInfoView) findViewById(aResourceID)).setWeatherIcon(imgURL); ((SingleWeatherInfoView) findViewById(aResourceID)).setWeatherString(aWFC.toString()); } //獲取天氣信息 //通過網(wǎng)絡(luò)獲取數(shù)據(jù) //傳遞給XMLReader解析 public void getCityWeather(URL url) { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); GoogleWeatherHandler gwh = new GoogleWeatherHandler(); xr.setContentHandler(gwh); InputStreamReader isr = new InputStreamReader(url.openStream(), "GBK"); InputSource is = new InputSource(isr); xr.parse(is); WeatherSet ws = gwh.getMyWeatherSet(); updateWeatherInfoView(R.id.weather_0, ws.getMyCurrentCondition()); updateWeatherInfoView(R.id.weather_1, ws.getMyForecastConditions().get(0)); updateWeatherInfoView(R.id.weather_2, ws.getMyForecastConditions().get(1)); updateWeatherInfoView(R.id.weather_3, ws.getMyForecastConditions().get(2)); updateWeatherInfoView(R.id.weather_4, ws.getMyForecastConditions().get(3)); } catch (Exception e) { Log.e("CityWeather", e.toString()); } }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -