Many times to use the Android side to invoke the WebService service, the following example is called WebService and convection in a variety of ways to deal with;
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;/** * This example implements the Android call WebService, and the network request , and Stream Processing * * @author Andy * Other content see http://blog.csdn.net/lyc66666666666 */public class Mainactivity extends Activity {@Ove rrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);d opost ();} private void DoPost () {/** * SOAP 1.2 complete request string replaced by the corresponding city and country * Post/globalweather.asmx Http/1.1host:www.webservicex.ne Tcontent-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> </CountryName> </GetWeather> </soap12:Body></soap12:Envelope> */new Thread () {public void run () {try {/** * ================================================== * HttpURLConnection mode access to the network is the simplest implementation, if complex network requests * such as with session cookie long connection or more complex request, please use Apache Open Source project HttpClient to implement * */inputstream is = GetXML (); Read Asset file string requestcontent = Readstream (is); requestcontent = SetValue (requestcontent, "Beijing");//Set parameters, Search weather in Beijing url url = new URL ("Http://www.webservicex.net/globalweather.asmx"); HttpURLConnection con = (httpurlconnection) url.openconnection () Con.setrequestmethod ("POST"); Con.setreadtimeout ( 30000); Con.setdooutput (true);//The following properties are Con.setrequestproperty ("Content-type", "application/soap+") that are set according to the request header of the SOAP protocol 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.getresponsecode () ==200 {InputStream instream = Con.getinputstream ();//Read input stream as binary 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 XML file in assets * * @return */private inputstream GetXML () {InputStream Inputstream;try {//Get binary stream of XML file InputStream = This.getresources (). Getassets (). Open ("Webservice.xml"); return InputStream;} catch (IOException e) {e.printstacktrace (); return null;}} /** * Read binary stream * @param inputstream * @return */private String readstream (InputStream inputstream) {try {InputStreamReader re Ader = 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 a placeholder FOR XML * @param XML * @param value * @return */private string setValue (string xml, String value) {string result = null;//Regular expression, matches $weather, and then replaces pattern pattern = pattern.compile ("\ \ $city"); Matcher Matcher = Pattern.matcher (XML), if (Matcher.find ()) {result = Matcher.replaceall (value);} return result;}}