Related Uses of WebService

Source: Internet
Author: User

Recent company projects use WebService, here is a simple summary.

In fact, detailed use of details in some cases need to change, but also need to see the actual situation, need to be linked with the server, detailed communication.

For example the company connects, not to put envelope.dotnet = true; Set to False, some <soap12:operation soapaction= "http://WebXml.com.cn/getCountryCityByIp " style="document"/> SOAPAction is empty. You cannot use SOAPAction to access, some pass-through is a JSON string ...

Very many public interfaces:http://www.webxml.com.cn/zh_cn/index.aspx


Here you use the Get Weather interface:

Go directly to the code.

Relatively simple, because of the time relationship, not specifically done. But in general how to use should still be able to see out.

However, it is better to study more deeply.


Project Structure diagram:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvehhtmjgyodi4/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">


Package com.example.utils;/** * <p> * Some required constants * </p> * PM 6:19:52 *  * @auther Dalvikcoder */public class Co nstants {/** namespace **/public static String name_space = "http://WebXml.com.cn/";/** webserviceserver address **/public static STR ing webservice_url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";/** wsdl address **/public static String Wsdl_url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";/** gets the supported city **/public static by province name String get_support_city = "getsupportcity";/** gets the supported province name **/public static String get_support_province = " Getsupportprovince ";/** get weather conditions for the next three days by city name **/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> * Tool class for WebService related operations * </p > * PM 5:20:51 * * @auther Dalvikcoder */public class Webserviceutils {/** * <p> * Get City Data * </p> * * @para M 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");//record last; position int end = Str.lastIndexOf (";"); /Take the string between start and end string temp = str.substring (start, end);//to divide the delimiter into an array 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 (",");p Rovince.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> * Send request * </p> * * @param soapobject * @param methodName * Corresponding method name * @return Soapobject please Beg Soapobject */public static Soapobject dorequest (Soapobject soapobject, String methodName) {Soapserializationenvelope Envelope = new Soapserializationenvelope (SOAPENVELOPE.VER10); Httptransportse Transportse = new Httptransportse (constants.wsdl_url,5 *); 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> * Get supported city name * </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 ("-------> Request return result", list.tostring ()); return list;} return null;} /** * <p> * Get a list of provinces * </p> * * @return list<citymodel> */public static list<string> Getsupportprov Ince () {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 ("-------> Request return result", resultstr); return list;} return null;} /** * <p> * for City Details * </p> * * @param thecityname * @return list<string> */public static void Getweath Erbycityname (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 ("------> Specific weather Information", weather);}}}

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvehhtmjgyodi4/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

Source Address: http://download.csdn.net/detail/xxm282828/7223667


Related Uses of WebService

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.