Android Call WebService Instance Analysis _android

Source: Internet
Author: User
Tags soap stub thread class

The example in this article describes how Android invokes WebService. Share to everyone for your reference. Specifically as follows:

WebService is a remote calling standard based on SOAP protocol, which can integrate different operating system platform, different languages and different technologies into one webservice. There is no library to invoke WebService in the Android SDK, so you need to use a Third-party SDK to invoke WebService. The PC version of the WebService client library is very rich, such as AXIS2,CXF, but these packages are too big for the Android system and may not be easy to migrate to the Android system. As a result, these development packages are not within our scope of consideration. Suitable for mobile phone WebService Client SDK has some, more commonly used to have KSOAP2, can download from http://code.google.com/p/ksoap2-android/downloads/list , copy the downloaded Ksoap2-android-assembly-2.4-jar-with-dependencies.jar package into the Lib directory of the Eclipse Project, and, of course, in other directories. This jar package is also referenced in the Eclipse project.

The Java code is as follows:

Package com.arg; 
Import java.util.ArrayList; 
Import Java.util.HashMap; 
Import Java.util.Iterator; 
Import java.util.List; 
Import Java.util.Map; 
Import Java.util.Map.Entry; 
Import android.app.Activity; 
Import Android.app.ProgressDialog; 
Import Android.os.Bundle; 
Import Android.os.Handler; 
Import Android.os.Message; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.widget.Button; 
Import Android.widget.EditText; 
Import Android.widget.ListView; 
Import Android.widget.SimpleAdapter; 
  public class Callwebserviceactivity extends activity {//Display results ListView ListView listview=null; 
  Enter text box EditText provinceedit=null; 
  A collection List list<map<string for storing data, object>> data=null; 
  Prompt dialog box ProgressDialog mydialog=null; 
 Search Buttons button searchbutton=null; /** called the activity is a. 
  * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview(R.layout.main); 
  Get text input box provinceedit= (edittext) This.findviewbyid (R.id.provinceedit);
  Get the Search button searchbutton= (button) This.findviewbyid (R.id.searchbutton); Add a click to the Search button Searchbutton.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {//Response button click 
    The function of the event Responseonclick ();
 }
  }); 
  //Response button Click the function of the event public void Responseonclick () {///Create a thread httpthread thread=new httpthread (handler); 
  Construct request Parameters HashMap <string,object> params=new hashmap<string,object> (); 
   try{charsequence Etvalue=provinceedit.gettext (); 
   String name= "";
   if (etvalue!=null) {//character-transcoding name=new String (etvalue.tostring (). GetBytes (), "UTF-8"); 
  } params.put ("Byprovincename", name); 
  }catch (Exception ex) {ex.printstacktrace (); 
  }//String url= "Http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx"; 
  String url = "Http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx"; String NAmespace = "http://WebXml.com.cn/"; 
  String methodname = "getsupportcity"; 
 Start a new thread for WebService request Thread.dostart (URL, nameSpace, methodname, params); /** * Capture Message Queuing */Handler handler=new Handler () {public void Handlemessage (messages m) {ArrayList <st 
   Ring> mylist= (arraylist<string>) M.getdata (). Getstringarraylist ("Data"); 
    if (MyList!=null) {if (data!=null) {data.clear (); 
    }else{data=new arraylist<map <string, object>> (); 
     for (int i=0;i<mylist.size (); i++) {map<string, object> item=new hashmap<string, object> (); 
     Item.put ("Text", Mylist.get (i)); 
    Data.add (item); 
    /** * * * * simpleadapter simpleadapter=new Simpleadapter (callwebserviceactivity.this 
    , Data,r.layout.listlayout,new string[] {"text"},new int []{r.id.showdata}); 
    listview= (ListView) Findviewbyid (R.id.showlistview); 
   Listview.setadapter (Simpleadapter); 
} 
  } }; 
  /** * Thread class * @author Administrator * * */public class Httpthread extends thread{private Handler handle=null; 
  String Url=null; 
  String Namespace=null; 
  String Methodname=null; 
  HashMap <string,object> params=null; 
  ProgressDialog Progressdialog=null; 
  Constructor public Httpthread (Handler hander) {Handle=hander; /** * Boot thread/public void Dostart (string url, String nameSpace, String methodname, Hashmap<string, 
   object> params) {//TODO auto-generated method stub this.url=url; 
   This.namespace=namespace; 
   This.methodname=methodname; 
   This.params=params; 
   Progressdialog=progressdialog.show (callwebserviceactivity.this, "hint", "request Please wait a moment ...", true); 
   This.start (); /** * Thread run/@Override public void Run () {//TODO auto-generated method stub System.out.println 
   ("Jack"); 
   Super.run (); 
    try{//web Service Request Soapobject result= (Soapobject) callwebservice (); Construction numberAccording to Arraylist<string> List=null; 
     If (Result!=null && result.getpropertycount () > 0) {list=new arraylist<string> (); 
      for (int i=0;i<result.getpropertycount (); i++) {soapprimitive value= (soapprimitive) result.getproperty (i);
     List.add (Value.tostring ()); 
     //a Cancel Progress dialog Box Progressdialog.dismiss (); 
     Constructs message message=handle.obtainmessage (); 
     Bundle b=new Bundle ();
     B.putstringarraylist ("Data", list); 
     Message.setdata (b); 
    Handle.sendmessage (message);
   }}catch (Exception ex) {ex.printstacktrace ();  }finally{}}/** * Request Web Service/protected Object Callwebservice () {String soap_action = NameSpace 
   + methodname; 
   Create a Soapobject instance soapobject request=new soapobject (namespace,methodname); 
   Generates a SOAP request message that invokes the Web service method Soapserializationenvelope envelope=new Soapserializationenvelope (SOAPENVELOPE.VER11); Set up. NET Web Service Envelope.dotneT=true; 
   Send requests Envelope.setoutputsoapobject (request); Request parameter if (params!= null &&!params.isempty ()) {for (iterator It=params.entryset (). iterator (); It.hasnext ( 
     );) {map.entry e= (Entry) it.next (); 
    Request.addproperty (E.getkey (). toString (), E.getvalue ());
   }//Androidhttptransport androidhttptrandsport=new androidhttptransport (URL);
   Soapobject Result=null; 
    try{//web Service Request Androidhttptrandsport.call (soap_action, envelope);
   Get the return result result= (soapobject) envelope.getresponse ();
   }catch (Exception ex) {ex.printstacktrace (); 
  return result; } 
 } 
}

I hope this article will help you with your Android program.

Related Article

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.