package sun.zone.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.ContextMenu; |
import android.view.Menu; |
import android.view.MenuItem; |
import android.view.View; |
import android.view.ContextMenu.ContextMenuInfo; |
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 |
{ |
|
private static final int First=Menu.FIRST; |
private static final int Second=Menu.FIRST+ 1 ; |
@Override |
public void onCreate(Bundle savedInstanceState) |
{ |
super .onCreate(savedInstanceState); |
setContentView(R.layout.main); |
|
//界面初始化方法 |
init(); |
} |
@Override |
public boolean onCreateOptionsMenu(Menu menu){ |
menu.add( 0 ,First, 1 , "播放音乐" ); |
menu.add( 0 ,Second, 2 , "停止播放" ); |
return true ; |
} |
@Override |
public boolean onOptionsItemSelected(MenuItem item){ |
// TODO Auto-generated method stub |
if (item.getItemId()== 1 ){ |
//开始播放音乐 |
|
} else if (item.getItemId()== 2 ){ |
//暂停音乐 |
|
} |
return super .onOptionsItemSelected(item); |
} |
//界面初始化方法实现 |
private void init() |
{ |
System.out.println( "开始界面初始化11111" ); |
//选择城市的下拉列表 |
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) |
{ |
Spinner spr = (Spinner) findViewById(R.id.Spinner01); |
Long l = spr.getSelectedItemId(); |
int index = l.intValue(); |
//获得代表城市名的代码 |
String cityParamString = ConstData.cityCode[index]; |
try |
{ |
/* |
* 通过URL获取网络资源 |
* 由于访问网络,所以要在AndroidManifest.xml中加入访问因特网服务的权限 |
* <uses-permission android:name="android.permission.INTERNET" /> |
*/ |
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()); |
} |
} |
|
}); |
} |
// 更新显示实时天气信息 |
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()); |
} |
// 更新显示天气预报 |
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()); |
} |
//获取天气信息 |
//通过网络获取数据 |
//由于获得的资源为xml文件则需要传递给XMLReader解析 |
public void getCityWeather(URL url) |
{ |
try |
{ |
System.out.println( "获取城市天气2222" ); |
//获取解析xml文件对象 |
SAXParserFactory spf = SAXParserFactory.newInstance(); |
SAXParser sp = spf.newSAXParser(); |
XMLReader xr = sp.getXMLReader(); |
|
|
//获取Handler对象并设置 |
GoogleWeatherHandler gwh = new GoogleWeatherHandler(); |
xr.setContentHandler(gwh); |
//打开网络资源连接并转化为流 |
InputStreamReader isr = new InputStreamReader(url.openStream(), "GBK" ); |
InputSource is = new InputSource(isr); |
System.out.println( "开始解析xml文件33333333" ); |
//解析数据流信息 |
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()); |
} |
} |
} |
初级程序员
by: 浅唱 发表于:2017-09-12 10:11:19 顶(0) | 踩(0) 回复
顶
回复评论