基本信息
源码名称:android 获取城市天气(google接口)
源码大小:0.07M
文件格式:.rar
开发语言:Java
更新时间:2016-02-18
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在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()); } } }