• WebService的相关使用


    近期公司项目使用WebService ,这里简单做个总结。

    事实上详细使用细节有些情况下须要改,还须要看实际情况,须要与server联调,详细沟通。

    比方公司连接,非要把envelope.dotNet = true; 设置为false,  某个<soap12:operation soapAction="http://WebXml.com.cn/getCountryCityByIp" style="document"/> soapAction为空的。就不能使用soapAction来訪问,有的传递參数是json字符串......

    公共接口非常多:http://www.webxml.com.cn/zh_cn/index.aspx


    这里使用获取天气接口:

    直接上代码吧。

    比較简单,因为时间关系,没有具体做。可是大体上怎样使用应该还是能够看的出来。

    但,还是研究比較深要好点。


    项目结构图:



    package com.example.utils;
    
    /**
     * <p>
     * 一些须要的常量
     * </p>
     * 下午6:19:52
     * 
     * @auther dalvikCoder
     */
    public class Constants {
    
    	/** 名字空间 **/
    	public static String NAME_SPACE = "http://WebXml.com.cn/";
    	/** webserviceserver地址 **/
    	public static String WEBSERVICE_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
    	/** wsdl地址 **/
    	public static String WSDL_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
    
    	/** 通过省份名称获取支持的城市 **/
    	public static String GET_SUPPORT_CITY = "getSupportCity";
    
    	/** 获取支持的省份名称 **/
    	public static String GET_SUPPORT_PROVINCE = "getSupportProvince";
    
    	/** 通过城市名称获取未来三天的天气情况 **/
    	public static String GET_WEATHER_BY_CITYNAME = "getWeatherbyCityName ";
    
    	
    }
    


    package com.example.utils;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpResponseException;
    import org.ksoap2.transport.HttpTransportSE;
    import org.xmlpull.v1.XmlPullParserException;
    
    import android.util.Log;
    
    import com.example.model.CityModel;
    import com.example.model.RegionProvince;
    
    /**
     * <p>
     * 用于WebService相关操作的工具类
     * </p>
     * 下午5:20:51
     * 
     * @auther dalvikCoder
     */
    public class WebServiceUtils {
    
    	/**
    	 * <p>
    	 * 获取城市数据
    	 * </p>
    	 * 
    	 * @param soapObject
    	 * @return List<RegionProvince>
    	 */
    	public synchronized List<RegionProvince> parseProvinceList(
    			SoapObject soapObject) {
    
    		List<RegionProvince> list = new ArrayList<RegionProvince>();
    		RegionProvince province = null;
    
    		String str = soapObject.getProperty(0).toString();
    
    		int start = str.indexOf("string");
    		// 记录最后一个;的位置
    		int end = str.lastIndexOf(";");
    
    		// 取start和end之间的字符串
    		String temp = str.substring(start, end);
    		// 以;为分隔符划分数组
    		String[] test = temp.split(";");
    		for (int i = 0; i < test.length; i++) {
    			province = new RegionProvince();
    			if (i == 0) {
    				temp = test[i].substring(7);
    			} else {
    				temp = test[i].substring(8);
    			}
    			int index = temp.indexOf(",");
    
    			province.setName(temp.substring(0, index));
    			temp = test[i].substring(11, 15);
    			province.setId(temp);
    
    			list.add(province);
    		}
    
    		return list;
    	}
    
    	public interface WebServiceCallBack {
    
    		void resultCallBack();
    	}
    
    	/**
    	 * <p>
    	 * 发送请求
    	 * </p>
    	 * 
    	 * @param soapObject
    	 * @param methodName
    	 *            相应的方法名称
    	 * @return SoapObject 请求SoapObject
    	 */
    	public static SoapObject doRequest(SoapObject soapObject, String methodName) {
    
    		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
    				SoapEnvelope.VER10);
    
    		HttpTransportSE transportSE = new HttpTransportSE(Constants.WSDL_URL,
    				5 * 1000);
    
    		envelope.setOutputSoapObject(soapObject);
    		envelope.dotNet = true;
    		envelope.encodingStyle = "UTF-8";
    		envelope.bodyOut = soapObject;
    		try {
    			transportSE.call(Constants.NAME_SPACE + methodName, envelope);
    			if (null != envelope.getResponse()) {
    
    				return (SoapObject) envelope.getResponse();
    
    			}
    		} catch (HttpResponseException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} catch (XmlPullParserException e) {
    			e.printStackTrace();
    		}
    
    		return null;
    	}
    
    	/**
    	 * <p>
    	 * 获取支持的城市名称
    	 * </p>
    	 * 
    	 * @param provinceName
    	 * @return List<CityModel>
    	 */
    	public static List<CityModel> getSupportCity(String provinceName) {
    
    		SoapObject soapObject = new SoapObject(Constants.NAME_SPACE,
    				Constants.GET_SUPPORT_CITY);
    
    		soapObject.addProperty("byProvinceName", provinceName);
    
    		SoapObject soapObject2 = WebServiceUtils.doRequest(soapObject,
    				Constants.GET_SUPPORT_CITY);
    
    		if (null != soapObject2) {
    
    			int count = soapObject2.getPropertyCount();
    			CityModel cityModel = null;
    			List<CityModel> list = new ArrayList<CityModel>();
    			for (int i = 0; i < count; i++) {
    
    				String str = soapObject2.getProperty(i).toString();
    				cityModel = new CityModel();
    				cityModel.setCityName(str.substring(0, 2));
    				cityModel.setCityCode(str.substring(4, 9));
    				list.add(cityModel);
    			}
    
    			Log.i("------->请求返回结果", list.toString());
    			return list;
    		}
    
    		return null;
    	}
    
    	/**
    	 * <p>
    	 * 获取省份列表
    	 * </p>
    	 * 
    	 * @return List<CityModel>
    	 */
    	public static List<String> getSupportProvince() {
    
    		SoapObject soapObject = new SoapObject(Constants.NAME_SPACE,
    				Constants.GET_SUPPORT_PROVINCE);
    
    		SoapObject soapObject2 = WebServiceUtils.doRequest(soapObject,
    				Constants.GET_SUPPORT_PROVINCE);
    
    		if (null != soapObject2) {
    
    			List<String> list = new ArrayList<String>();
    			String resultStr = soapObject2.getPropertyAsString(0).toString();
    			int count = soapObject2.getPropertyCount();
    			for (int i = 0; i < count; i++) {
    
    				list.add(soapObject2.getProperty(i).toString());
    
    			}
    			Log.i("------->请求返回结果", resultStr);
    			return list;
    
    		}
    
    		return null;
    	}
    
    	/**
    	 * <p>
    	 * 获取城市具体信息
    	 * </p>
    	 * 
    	 * @param theCityName
    	 * @return List<String>
    	 */
    	public static void getWeatherbyCityName(String theCityName) {
    
    		SoapObject soapObject = new SoapObject(Constants.NAME_SPACE,
    				Constants.GET_WEATHER_BY_CITYNAME);
    
    		soapObject.addProperty("theCityName", theCityName);
    		SoapObject soapObject2 = WebServiceUtils.doRequest(soapObject,
    				Constants.GET_WEATHER_BY_CITYNAME);
    
    		if (null != soapObject2) {
    
    			String weather = soapObject2.toString();
    			Log.e("------>具体具体天气信息", weather);
    		}
    
    	}
    
    }


    源码地址:http://download.csdn.net/detail/xxm282828/7223667


  • 相关阅读:
    WPF常用TriggerAction用法 (一)
    一个WPF只能输入数字的行为。
    自定义panel实现,并实现item更改和移除动画。
    MVVM模式下弹出窗体
    ZAM 3D 制作简单的3D字幕 流程(二)
    ZAM 3D 制作简单的3D字幕 流程(一)
    ZAM 3D 制作3D动画字幕 用于Xaml导出
    Metro Win8风格的按钮(Filp翻转)
    WPF自动隐藏的消息框(鼠标放上去将一直显示,移开动画继续),提供normal和error两种边框。
    可分组的选择框控件(MVVM下)(Toggle样式 仿造单选框RadioButton,复选框CheckBox功能)
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5154705.html
Copyright © 2020-2023  润新知