用户注册



邮箱:

密码:

用户登录


邮箱:

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

发表随想


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

获取天气预报(天气,气温,风力...)WebService

2012-10-26 作者: 小蜜锋举报

[android]代码库

/**
 * 获取天气预报(天气,气温,风力...)
 */
public class MainActivity extends Activity {

	private static final String NAMESPACE = "http://WebXml.com.cn/";
	// WebService地址 后面的?wsdl根据WebService地宫的地址可加可不加
	private static String URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
	private static final String METHOD_NAME = "getWeatherbyCityName";
	private static String SOAP_ACTION = NAMESPACE + METHOD_NAME;

	private String weatherToday;

	private Button okButton;
	private SoapObject detail;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork() // 这里可以替换为detectAll()
																		// 就包括了磁盘读写和网络I/O
				.penaltyLog() // 打印logcat,当然也可以定位到dropbox,通过文件保存相应的log
				.build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects() // 探测SQLite数据库操作
				.penaltyLog() // 打印logcat
				.penaltyDeath().build());

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		okButton = (Button) findViewById(R.id.ok);

		okButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				showWeather();
			}
		});
	}

	private void showWeather() {
		String city = "郑州";
		getWeather(city);
	}

	public void getWeather(String cityName) {
		try {
			SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
			rpc.addProperty("theCityName", cityName);

			HttpTransportSE ht = new HttpTransportSE(URL);
			ht.debug = true;

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);

			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);

			ht.call(SOAP_ACTION, envelope);
			// ht.call(null, envelope);

			SoapObject result = (SoapObject) envelope.bodyIn;
			detail = (SoapObject) result
					.getProperty("getWeatherbyCityNameResult");

			System.out.println("result" + result);
			System.out.println("detail" + detail);
			Toast.makeText(MainActivity.this, detail.toString(),
					Toast.LENGTH_LONG).show();
			parseWeather(detail);

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

	private void parseWeather(SoapObject detail)
			throws UnsupportedEncodingException {
		String date = detail.getProperty(6).toString();
		weatherToday = "今天:" + date.split(" ")[0];
		weatherToday = weatherToday + "\n天气:" + date.split(" ")[1];
		weatherToday = weatherToday + "\n气温:"
				+ detail.getProperty(5).toString();
		weatherToday = weatherToday + "\n风力:"
				+ detail.getProperty(7).toString() + "\n";
		System.out.println("weatherToday is " + weatherToday);
		Toast.makeText(MainActivity.this, weatherToday, Toast.LENGTH_LONG)
				.show();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);

		return true;
	}
}


网友评论    (发表评论)

共6 条评论 1/1页

发表评论:

评论须知:

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


扫码下载

加载中,请稍后...

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

加载中,请稍后...