用户注册



邮箱:

密码:

用户登录


邮箱:

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

发表随想


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

Android天气预报源码AndroidWeatherForecast

2013-01-30 作者: 小蜜锋举报

[android]代码库

package com.xmobileapp.android.weatherforecast;

import java.io.StringBufferInputStream;
import java.util.HashMap;
import java.util.Map;

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.content.Intent;
import android.os.Bundle;
import android.sax.Element;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

import com.xmobileapp.android.weatherforecast.meta.CityWeather;
import com.xmobileapp.android.weatherforecast.meta.Constant;
import com.xmobileapp.android.weatherforecast.transport.WebServiceCaller;

public class WeatherResult extends Activity {

	private CityWeather cityWeather;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);

		TextView tv = (TextView) this.findViewById(R.id.resultText);

		cityWeather = new CityWeather();
		final StringBuffer sb = new StringBuffer();

		Intent queryIntent = getIntent();
		String cityName = queryIntent.getStringExtra("CityName");
		// String countryName = queryIntent.getStringExtra("CountryName");

		Map map = new HashMap();
		map.put("CityName", cityName);
		map.put("CountryName", "China");
		String xmlStr = WebServiceCaller.doCallWebService(
				Constant.SOAP_ACTION2, Constant.METHOD_NAME2, map);
		// Log.d("xmlStr", xmlStr);

		RootElement root = new RootElement("CurrentWeather");
		Element entry = root.getChild("Location");
		entry.setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				System.out.println("prop: " + body);
				cityWeather.setLocation(body);
				sb.append("位置:" + body + "\n");
			}
		});
		Element entry2 = root.getChild("Time");
		entry2.setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				System.out.println("prop: " + body);
				cityWeather.setTime(body);
				sb.append("时间:" + body + "\n");
			}
		});
		Element entry3 = root.getChild("Wind");
		entry3.setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				System.out.println("prop: " + body);
				cityWeather.setWind(body);
				sb.append("风向:" + body + "\n");
			}
		});
		Element entry4 = root.getChild("Visibility");
		entry4.setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				System.out.println("prop: " + body);
				cityWeather.setVisibility(body);
				sb.append("能见度:" + body + "\n");
			}
		});
		Element entry5 = root.getChild("Temperature");
		entry5.setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				System.out.println("prop: " + body);
				cityWeather.setTemperature(body);
				sb.append("气温:" + body + "\n");
			}
		});
		Element entry6 = root.getChild("DewPoint");
		entry6.setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				System.out.println("prop: " + body);
				cityWeather.setDewPoint(body);
				// sb.append("位置:"+body+"\n");
			}
		});
		Element entry7 = root.getChild("RelativeHumidity");
		entry7.setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				System.out.println("prop: " + body);
				cityWeather.setRelativeHumidity(body);
				sb.append("相对湿度:" + body + "\n");
			}
		});
		Element entry8 = root.getChild("Pressure");
		entry8.setEndTextElementListener(new EndTextElementListener() {
			public void end(String body) {
				System.out.println("prop: " + body);
				cityWeather.setPressure(body);
				sb.append("气压:" + body + "\n");
			}
		});

		try {

			// create the factory
			SAXParserFactory factory = SAXParserFactory.newInstance();
			// create a parser
			SAXParser parser = factory.newSAXParser();

			XMLReader xmlreader = parser.getXMLReader();
			xmlreader.setContentHandler(root.getContentHandler());

			// get our data via the xml string
			InputSource is = new InputSource(
					new StringBufferInputStream(xmlStr));

			xmlreader.parse(is);

		} catch (Exception e) {
			e.printStackTrace();

		}

		tv.setText(sb);
	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// if (choiceMode == true) {
	// MenuInflater inflater = getMenuInflater();
	// inflater.inflate(R.menu.gal_menu, menu);
	// menu.findItem(R.id.GAL_menu_OK_Item).setEnabled(false);
	// }
	// return true;
	// }
	//
	// @Override
	// public boolean onMenuItemSelected(int featureId, MenuItem item) {
	// // TODO Auto-generated method stub
	// return super.onMenuItemSelected(featureId, item);
	// }

}

[代码运行效果截图]


Android天气预报源码AndroidWeatherForecast

[源代码打包下载]




网友评论    (发表评论)

共3 条评论 1/1页

发表评论:

评论须知:

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


扫码下载

加载中,请稍后...

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

加载中,请稍后...