Most of the time, you need to use the android client to call the webservice. The following example shows how to call webservice and stream processing;
Package com. example. android_webservice; import java. io. bufferedReader; import java. io. byteArrayOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java. io. outputStream; import java.net. httpURLConnection; import java.net. URL; import java. util. regex. matcher; import java. util. regex. pattern; import android. app. activity; import android. OS. bundle;/*** implement a in this example Ndroid client calls webservice, as well as network requests, and stream processing ** @ author andy ** other content see http://blog.csdn.net/lyc66666666666 */public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); doPost ();} private void doPost () {/*** Replace the complete request string of soap 1.2 with the corresponding city and country ** POST/globalweather. asmx HTTP/1.1 Host: www. webser Vicex. netContent-Type: application/soap + xml; charset = utf-8Content-Length: length <? Xml version = "1.0" encoding = "UTF-8"?> <Soap12: Envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd = "http://www.w3.org/2001/XMLSchema" xmlns: soap12 = "http://www.w3.org/2003/05/soap-envelope"> <soap12: body> <GetWeather xmlns = "http://www.webserviceX.NET"> <CityName> string </CityName> <CountryName> </GetWeather> </soap12: Body> </soap12: envelope> */new Thread () {public void run () {try {/*** ================== =======================================================* HttpURLConnection is the most simple implementation, if complex network requests * such as persistent connections with session cookies or more complex requests, use the apache open-source project httpclient to implement **/InputStream is = getXML (); // read the asset file String requestContent = readStream (is); requestContent = setValue (requestContent, "beijing"); // set the parameter, query weather in Beijing URL = new url ("http://www.webservicex.net/globalweather.asmx"); HttpURLConnection con = (HttpURLConnection) URL. OpenConnection (); con. setRequestMethod ("POST"); con. setreadtimeouts (30000); con. setDoOutput (true); // the following attributes are con set based on the soap request header. setRequestProperty ("Content-Type", "application/soap + xml; charset = UTF-8"); con. setRequestProperty ("Content-Length", String. valueOf (requestContent. getBytes (). length); OutputStream OS = con. getOutputStream (); OS. write (requestContent. getBytes (); OS. flush (); OS. close (); if (con. getRespo NseCode () == 200) {InputStream instream = con. getInputStream (); // read the input stream ByteArrayOutputStream byteOS = new ByteArrayOutputStream (); int len =-1; byte [] buffer = new byte [1024]; while (instream. read (buffer )! =-1) {byteOS. write (buffer, 0, len);} System. out. println ("returned result:" + new String (byteOS. toByteArray ();} catch (Exception e) {e. printStackTrace ();}};}. start ();}/*** get the xml file in assets ** @ return */private InputStream getXML () {InputStream inputstream; try {// obtain the binary stream inputstream of the xml file = this. getResources (). getAssets (). open ("webservice. xml "); return inputstream;} catch (IOException e) {e. printStackTrace (); re Turn null;}/*** read binary stream * @ param inputstream * @ return */private String readStream (InputStream inputstream) {try {InputStreamReader reader = new InputStreamReader (inputstream ); bufferedReader bufferedReader = new BufferedReader (reader); StringBuffer sb = new StringBuffer (); char [] buffer = new char [512]; int length = bufferedReader. read (buffer); while (length! =-1) {sb. append (new String (buffer); length = bufferedReader. read (buffer);} return sb. toString ();} catch (Exception e) {e. printStackTrace (); return null ;}/ *** fill in the placeholder value * @ param xml * @ param value * @ return */private String setValue (String xml, string value) {String result = null; // regular expression, matching $ weather, and replacing Pattern pattern = Pattern. compile ("\ $ city"); Matcher matcher = pattern. matcher (xml); if (matcher. find () {result = matcher. replaceAll (value) ;}return result ;}}
The following is the xml file in the android assets package, which contains the requested webservice information;
<?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <GetWeather xmlns="http://www.webserviceX.NET"> <CityName>&city</CityName> <CountryName> </CountryName> </GetWeather> </soap12:Body> </soap12:Envelope>