加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService的使用

发布时间:2020-12-17 00:05:33 所属栏目:安全 来源:网络整理
导读:首先导包?ksoap2-android-assembly-3.0.0-jar-with-dependencies.jar,构建路径。 使用方法: (1) 指定webservice的命名空间和调用的方法名 (2)设置调用方法的参数值,如果没有参数,可以省略 (3) 生成调用Webservice方法的SOAP请求信息。该信息由Soap

首先导包?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;
	}
}

? ? ? ? ? ? ? ? ?? ? ??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读