WebService的相关使用

2020-12-13 02:53

阅读:391

标签:android开发   webservice   

最近公司项目使用WebService ,这里简单做个总结。 其实具体使用细节有些情况下需要改,还需要看实际情况,需要与服务器联调,具体沟通。比如公司连接,非要把envelope.dotNet = true; 设置为false,  某个 soapAction="http://WebXml.com.cn/getCountryCityByIp" style="document"/> soapAction为空的,就不能使用soapAction来访问,有的传递参数是json字符串......

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


这里使用获取天气接口:

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


项目结构图:

soscw.com,搜素材


package com.example.utils;

/**
 * 

* 一些需要的常量 *

* 下午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;

/**
 * 

* 用于WebService相关操作的工具类 *

* 下午5:20:51 * * @auther dalvikCoder */ public class WebServiceUtils { /** *

* 获取城市数据 *

* * @param soapObject * @return List */ public synchronized List parseProvinceList( SoapObject soapObject) { List list = new ArrayList(); 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 * 发送请求 * * * @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; } /** *

* 获取支持的城市名称 *

* * @param provinceName * @return List */ public static List 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 list = new ArrayList(); for (int i = 0; i 请求返回结果", list.toString()); return list; } return null; } /** *

* 获取省份列表 *

* * @return List */ public static List 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 list = new ArrayList(); String resultStr = soapObject2.getPropertyAsString(0).toString(); int count = soapObject2.getPropertyCount(); for (int i = 0; i 请求返回结果", resultStr); return list; } return null; } /** *

* 获取城市详细信息 *

* * @param theCityName * @return List */ 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); } } }

soscw.com,搜素材

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


WebService的相关使用,搜素材,soscw.com

WebService的相关使用

标签:android开发   webservice   

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


评论


亲,登录后才可以留言!