Android webService访问实例

  提问: 6 年 前 最后更新: 4 个月 前 浏览数: 16239
2
4
参考网上的例子实现一个简单的天气查看功能。 界面包含一个按钮,当点击按钮时 已tips 提示框的方式展现天气信息。
package com.lht.WebService;

import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.os.Bundle;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class WebService extends Activity {
	private static final String NAMESPACE = "http://WebXml.com.cn/";
	// WebService地址
	private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
	private static final String METHOD_NAME = "getWeatherbyCityName";
	private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";

	private String weatherToday;

	private Button okButton;
	private SoapObject detail;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.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);
	}

	@SuppressWarnings("deprecation")
	public void getWeather(String cityName) {
		try {
			System.out.println("rpc------");
			SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
			System.out.println("rpc" + rpc);
			System.out.println("cityName is " + cityName);
			rpc.addProperty("theCityName", cityName);

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

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);

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

			ht.call(SOAP_ACTION, envelope);

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

			System.out.println("result" + result);
			System.out.println("detail" + detail);
			Toast.makeText(WebService.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(WebService.this, weatherToday, Toast.LENGTH_LONG).show();

	}
}

提问时间 2011-11-01 09:48

fmms的头像

fmms
0 5 7
答案被采用率: 14.285714285714285%

这不是JAVA编写的程序么
2 年 前 1069374674

学习收藏了

回答于 2014-06-08 21:58

sz_lrp2014的头像

sz_lrp2014
0 0 1
答案被采用率: 0.0%

what 

回答于 2017-12-21 18:20

mytestnone的头像

mytestnone
0 0 4
答案被采用率: 0.0%

很好

回答于 2015-05-20 15:25

956825718的头像

956825718
0 0 1
答案被采用率: 0.0%

学习学习

回答于 2014-08-27 21:01

zlpzlpzlp的头像

zlpzlpzlp
0 0 5
答案被采用率: 0.0%

收藏

回答于 2015-08-20 16:52

473860369的头像

473860369
0 0 2
答案被采用率: 0.0%

学习了学习了

回答于 2011-12-23 09:58

zui的头像

zui
0 0 2
答案被采用率: 0.0%

又在哪里转载的。

回答于 2011-12-23 13:48

saiya520的头像

saiya520
0 0 2
答案被采用率: 0.0%

您的回答:

  

powered by Open-Open.com