微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WebService的使用

首先导包 ksoap2-android-assembly-3.0.0-jar-with-dependencies.jar,构建路径。

使用方法

(1) 指定webservice的命名空间和调用方法

(2)设置调用方法的参数值,如果没有参数,可以省略

(3) 生成调用Webservice方法的SOAP请求信息。该信息由SoapSerializationEnvelope对象描述

(4) 创建HttpTransportsSE对象。通过HttpTransportsSE类的构造方法可以指定WebService的WSDL文档的URL

(5)使用call方法调用WebService方法

(6)使用getResponse方法获得WebService方法的返回结果

配置网络访问权限:<uses-permission android:name="android.permission.INTERNET" />


代码演示:

package com.example.testcanvas;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.ksoap2.soapEnvelope;
import org.ksoap2.serialization.soapObject;
import org.ksoap2.serialization.soapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class WebServiceTest extends Activity{

	private Button btn;
	private TextView tv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// Todo Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weather);
		btn = (Button) findViewById(R.id.weather_button1);
		tv = (TextView) findViewById(R.id.weather_textView1);
		btn.setonClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String cityName = "成都";
				String s = getWeather(cityName);
				tv.setText(s);
			}
		});
	}
	//命名空间
	private static String NAMESPACE = "http://WebXml.com.cn/";
	//调用方法
	private static String METHOD_NAME="getWeatherbyCityName";
	//WebService的地址
	private static String url = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
	private String weatherToday;
	private SoapObject detail;
	//根据城市获取天气信息的网址
	private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";

	/**
	 * 获得天气信息
	 * @param cityName 城市名称
	 * @return
	 */
	public String getWeather(String cityName){
		String  s = "";
		try {
			SoapObject soap = new SoapObject(NAMESPACE,METHOD_NAME);
			soap.addPropertyIfValue("theCityName",cityName);
			//生成调用WebService方法的SOAP请求信息
			SoapSerializationEnvelope envleope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envleope.bodyOut=soap;
			envleope.dotNet=true;
			envleope.setoutputSoapObject(soap);
			//访问服务器
			HttpTransportSE ht = new HttpTransportSE(url);
			//使用call方法调用WebService方法
			ht.call(SOAP_ACTION,envleope);
			ht.debug=true;
			//得到响应天气数据
			detail = (SoapObject)envleope.getResponse();
			//服务器返回一个一维数组 String(22),共有23个元素。
			//String(0) 到 String(4):省份,城市,城市代码,城市图片名称,最后更新时间。
			//String(5) 到 String(11):当天的 气温,概况,风向和风力,天气趋势开始图片名称,天气趋势结束图片名称,现在的天气实况,天气和生活指数。
			//String(12) 到 String(16):第二天的 气温,概况,风向和风力。
			//String(17) 到 String(21):第三天的 气温,概况,风向和风力。
			//String(22) 被查询的城市或地区的介绍 
			Toast.makeText(this,detail.toString(),Toast.LENGTH_LONG).show();
			s = parseWeather(detail);
		} catch (IOException e) {
			e.printstacktrace();
		} catch (XmlPullParserException e) {
			e.printstacktrace();
		}
		return s;
	}
	/**
	 * 获得天气信息中一部分信息
	 * @param detail
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	private String 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();
		return weatherToday;
	}
}

                       

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐