首页

android 获取城市天气(google接口)

java

2020-8-2

获取天气的接口是google的,需要网络能访问google

public static final String queryString=”http://www.google.com/ig/api?hl=zh-cn&weather=,,,”;
public static final String queryString_intput=”http://www.google.com/ig/api?hl=zh_cn&weather=”;

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


	// 更新显示实时天气信息
	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());
	}
	
	//获取天气信息
	//通过网络获取数据
	//传递给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());
		}
	}
}
资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
CityWeather/.classpath , 280
CityWeather/.project , 848
CityWeather/AndroidManifest.xml , 774
CityWeather/bin/CityWeather.apk , 20733
CityWeather/bin/classes.dex , 16992
CityWeather/bin/com/yarin/android/CityWeather/CityWeather$1.class , 1922
CityWeather/bin/com/yarin/android/CityWeather/CityWeather$2.class , 1702
CityWeather/bin/com/yarin/android/CityWeather/CityWeather.class , 4902
CityWeather/bin/com/yarin/android/CityWeather/ConstData.class , 2787
CityWeather/bin/com/yarin/android/CityWeather/GoogleWeatherHandler.class , 3596
CityWeather/bin/com/yarin/android/CityWeather/R$attr.class , 373
CityWeather/bin/com/yarin/android/CityWeather/R$color.class , 453
CityWeather/bin/com/yarin/android/CityWeather/R$drawable.class , 459
CityWeather/bin/com/yarin/android/CityWeather/R$id.class , 1140
CityWeather/bin/com/yarin/android/CityWeather/R$layout.class , 427
CityWeather/bin/com/yarin/android/CityWeather/R$string.class , 516
CityWeather/bin/com/yarin/android/CityWeather/R.class , 667
CityWeather/bin/com/yarin/android/CityWeather/SingleWeatherInfoView.class , 2493
CityWeather/bin/com/yarin/android/CityWeather/WeatherCurrentCondition.class , 2000
CityWeather/bin/com/yarin/android/CityWeather/WeatherForecastCondition.class , 1720
CityWeather/bin/com/yarin/android/CityWeather/WeatherSet.class , 1439
CityWeather/bin/resources.ap_ , 11199
CityWeather/default.properties , 449
CityWeather/gen/com/yarin/android/CityWeather/R.java , 2226
CityWeather/res/drawable/bg.jpg , 1898
CityWeather/res/drawable/icon.png , 7405
CityWeather/res/layout/main.xml , 3918
CityWeather/res/values/strings.xml , 332
CityWeather/src/com/yarin/android/CityWeather/CityWeather.java , 3942
CityWeather/src/com/yarin/android/CityWeather/ConstData.java , 2165
CityWeather/src/com/yarin/android/CityWeather/GoogleWeatherHandler.java , 5368
CityWeather/src/com/yarin/android/CityWeather/SingleWeatherInfoView.java , 1749
CityWeather/src/com/yarin/android/CityWeather/WeatherCurrentCondition.java , 2218
CityWeather/src/com/yarin/android/CityWeather/WeatherForecastCondition.java , 1484
CityWeather/src/com/yarin/android/CityWeather/WeatherSet.java , 1038
没有账号? 忘记密码?

社交账号快速登录