关闭

webService的用法简介

57人阅读 评论(0) 收藏 举报
<span style="font-size:18px;">导入ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar包</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">public class MainActivity extends Activity {
//	String key = "5ae00c9c22fa435ba56161ef0d263b7c";
	//要访问的方法
	String name = "GetWeather";
	//命名空间
	String namespace = "http://www.36wu.com/";

	String url = "http://web.36wu.com/WeatherService.asmx?WSDL";
	
	String soapAction = "http://www.36wu.com/GetWeather";
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//android4.0之后,主线程不可以做联网操作
	new Thread() {

		public void run() {
			getWeather();
		};

	}.start();

	}

	/**
	 * 联网获得天气情况
	 */
	private void getWeather() {
		// 得到HttpTransportSE对象,设置请求地址
		HttpTransportSE httpTransportSE = new HttpTransportSE(url);
		//得到SoapSerializationEnvelope,携带与服务器通信的信息
         SoapSerializationEnvelope serializationEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
       //得到SoapObject对象,主要携带要传递给服务器的请求信息(键值对的形式),
         SoapObject soapObject_out=new SoapObject(namespace, name);
//         (键值对的形式携带信息),
         soapObject_out.addProperty("district", "太原");
         soapObject_out.addProperty("authkey", "5ae00c9c22fa435ba56161ef0d263b7c");
         //设置传递给服务器的信息
         serializationEnvelope.bodyOut=soapObject_out;
         //设置对.NET语言更好的支持
 		serializationEnvelope.dotNet = true;

		try {
			// 开始访问
			httpTransportSE.call(soapAction, serializationEnvelope);
			// 得到服务器返回的数据
			SoapObject soapObject_In = (SoapObject) serializationEnvelope.bodyIn;

			System.out.println("整体信息==" + soapObject_In.toString());

			// 获得Key是GetWeatherResult的信息
			SoapObject getWeatherResult = (SoapObject) soapObject_In
					.getProperty("GetWeatherResult");
			System.out.println("getWeatherResult=="
					+ getWeatherResult.toString());

			// 获得Key是data的信息
			SoapObject data = (SoapObject) getWeatherResult.getProperty("data");

			System.out.println("data==" + data.toString());
			
			/*
			 * int propertyCount = data.getPropertyCount();
			 * 
			 * System.out.println(propertyCount+"条数据");
			 */
			
			// 获得Key是minTemp的信息
			Object minTemp = data.getProperty("minTemp");
			System.out.println(minTemp + "度");

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}</span>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2294次
    • 积分:249
    • 等级:
    • 排名:千里之外
    • 原创:24篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条