码迷,mamicode.com
首页 > Web开发 > 详细

WebService的相关使用

时间:2014-05-11 03:29:20      阅读:534      评论:0      收藏:0      [点我收藏+]

标签:android开发   webservice   

最近公司项目使用WebService ,这里简单做个总结。 其实具体使用细节有些情况下需要改,还需要看实际情况,需要与服务器联调,具体沟通。比如公司连接,非要把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


这里使用获取天气接口:

直接上代码吧。比较简单,由于时间关系,没有详细做。但是大体上如何使用应该还是可以看的出来。但,还是研究比较深要好点。


项目结构图:

bubuko.com,布布扣


package com.example.utils;

/**
 * <p>
 * 一些需要的常量
 * </p>
 * 下午6:19:52
 * 
 * @auther dalvikCoder
 */
public class Constants {

	/** 名字空间 **/
	public static String NAME_SPACE = "http://WebXml.com.cn/";
	/** webservice服务器地址 **/
	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);
		}

	}

}

bubuko.com,布布扣

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


WebService的相关使用,布布扣,bubuko.com

WebService的相关使用

标签:android开发   webservice   

原文地址:http://blog.csdn.net/xxm282828/article/details/25507505

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!