用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

天气查询

2014-07-22 作者: 云代码会员举报

[android]代码库

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());
		}
	}
}

[代码运行效果截图]


天气查询

[源代码打包下载]




网友评论    (发表评论)

共3 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...