Android calls WebService interface for information

Source: Internet
Author: User

I have a blog on how to build a Webservice,service layer based on CXF interface will be deployed to Tomcat, this article I will talk about how to invoke these interfaces in wirelessly pass parameters.

1. Insert the KSOAP2 jar package in Lib and import

2. In the XML configuration file, add:

<!--permissions to access the network--      

3. The next step is to invoke the code by calling an excuse

String Wsdl_uri = "http://localhost:8080/WebService4/getNewIdPort?wsdl";//URI of the WSDLString namespace = "http://services.he.TSD/";//namespaceString methodName = "Getnew";//the name of the method to invoke//Create a Httptransportse objectHttptransportse HT =NewHttptransportse (Wsdl_uri); Ht.debug=true;//creating envelop objects using the soap1.1 protocolSoapserializationenvelope envelope =NewSoapserializationenvelope (SOAPENVELOPE.VER11);//instantiating a Soapobject objectSoapobject Object =NewSoapobject (namespace, methodName);//Setting ParametersObject.addproperty ("arg0", "HHH");//set the Soapobject object to an outgoing SOAP message for the Soapserializationenvelope objectEnvelope.bodyout = object;//because it is a send request, it is set bodyoutEnvelope.dotnet =false; Envelope.setoutputsoapobject (object);Try{  //Call WebServiceHt.call (NULL, envelope);System.out.println ("Value of callback:" +envelope.getresponse ());}Catch(Exception e) {e.printstacktrace (); String msg=E.getmessage (); SYSTEM.OUT.PRINTLN (msg);}

4. If WebService does not receive the value from Android:

(1) The name of the parameter is not correct

(2). NET WebService to be envelope.dotnet =true, otherwise envelope.dotnet =false

5. Because it is deployed to Tomcat, to be in a network segment, the phone and the computer have a WiFi connection. IPv4 find with Window+r, then cmd, the last ipconfig can be found.

6. To turn off Windows Firewall

7. I was also confronted with the problem that when the information passed was complex, such as passing a custom class of list type, I was rewriting the ListAdapter class as follows:

 PackageCom.adapter;/*** Created by Hexiaolu on 2017/4/13.*/Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.os.Bundle;Importandroid.text.Editable;ImportAndroid.text.TextWatcher;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.EditText;ImportAndroid.widget.ListView;ImportAndroid.widget.TextView;ImportCOM.EXAMPLE.ADMINISTRATOR.STOCK2.R; Public classListAdapterextendsbaseadapter {HASHMAP Item=NewHashMap (); PrivateLayoutinflater Minflater; //private list<map<string, object>> Mdata;//the stored edittext value    PrivateArrayList Mdata=NewArrayList (); //private ArrayList mdate = new ArrayList ();     Publicmap<string, string> editorvalue =NewHashmap<string, string> ();//    PrivateTextView Spendmoney;  PublicListAdapter (Context context, ArrayList Data,textview spend) {Spendmoney=spend; Mdata=data; Minflater=Layoutinflater.from (context);    Init (); }    //Initialize    Private voidinit () {editorvalue.clear (); } @Override Public intGetCount () {returnmdata.size (); } @Override PublicObject GetItem (intposition) {        return NULL; } @Override Public LongGetitemid (intposition) {        return0; }    PrivateInteger index =-1; @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {Viewholder Holder=NULL; //initializes the Convertview when Convertview is null.         if(Convertview = =NULL) {Holder=NewViewholder (); Convertview= Minflater.inflate (R.layout.list_item,NULL); Holder.goodsname=(TextView) convertview. Findviewbyid (R.id.list_item_goodsname); Holder.goodssellprice=(TextView) convertview. Findviewbyid (R.id.list_item_goodssellprice); Holder.goodsunit=(TextView) convertview. Findviewbyid (R.id.list_item_goodsunit); Holder.goodssellnum=(EditText) convertview. Findviewbyid (R.id.list_item_goodssellnum);            Holder.goodsSellNum.setTag (position); /*Holder.goodsSellNum.setOnTouchListener (New Ontouchlistener () {@Override public boolean                        OnTouch (View V, motionevent event) {if (event.getaction () = = motionevent.action_up) {                    index = (Integer) v.gettag ();                } return false; }            });*/            classMytextwatcherImplementsTextwatcher { PublicMytextwatcher (Viewholder holder) {Mholder=Holder; }                PrivateViewholder Mholder; @Override Public voidOnTextChanged (Charsequence S,intStart,intBefore,intcount) {} @Override Public voidBeforetextchanged (Charsequence S,intStart,intCountintAfter ) {} @Override Public voidaftertextchanged (Editable s) {if(s! =NULL&& "". Equals (S.tostring ())) {                        intPosition =(Integer) MHolder.goodsSellNum.getTag (); /*mdata.get (position). Put ("Goodssellnum", S.tostring ()), or//////When the EditText data changes*/Item=(HASHMAP) mdata.get (position); Item.put ("Goodssellnum", s.tostring ());                        Mdata.set (Position,item); Spendmoney.settext (string.valueof (Calculate ()));//Calculate Total Price}}} holder.goodsSellNum.addTextChangedListener (Newmytextwatcher (holder));        Convertview.settag (holder); } Else{Holder=(Viewholder) Convertview.gettag ();        Holder.goodsSellNum.setTag (position); } Item=(HASHMAP) mdata.get (position); Object value=item.get ("Goodsname"); //Object value = mdata.get (position). Get ("List_item_goodsname");        if(Value! =NULL) {holder.goodsName.setText (String) value); } Item=(HASHMAP) mdata.get (position); Value=item.get ("Goodssellprice"); //value = mdata.get (position). Get ("List_item_goodssellprice");        if(Value! =NULL) {Holder.goodsSellPrice.setText (value.tostring ()); } Item=(HASHMAP) mdata.get (position); Value=item.get ("Goodsunit"); //value = mdata.get (position). Get ("List_item_goodsunit");        if(Value! =NULL) {Holder.goodsUnit.setText (value.tostring ()); } Item=(HASHMAP) mdata.get (position); Value=item.get ("Goodssellnum"); //value = mdata.get (position). Get ("List_item_goodssellnum");        if(Value! =NULL&& "". Equals (value))        {Holder.goodsSellNum.setText (value.tostring ());        } holder.goodsSellNum.clearFocus (); if(Index! =-1 && index = =position)        {Holder.goodsSellNum.requestFocus (); }        returnConvertview; }     Public Final classViewholder { PublicTextView Goodsname;  PublicTextView Goodssellprice;  PublicEditText Goodssellnum;//input in the ListView         PublicTextView Goodsunit;//used to define the iconic primary key, you don't have to care    }     Public floatcalculate () {floatMoney = 0;  for(inti = 0; I < mdata.size (); i++) {HASHMAP Item=NewHashMap (); Item=(HASHMAP) mdata.get (i); floatnum = float.parsefloat (item.get ("Goodssellnum"). toString ()); floatPrice = Float.parsefloat (Item.get ("Goodssellprice"). toString ()); Money= Money + num *Price ; }        returnMoney ; }}
View Code

8. Tune how to use the ListAdapter class

New ListAdapter ( this
Lv_goodslist.setadapter (adapter);

Android calls WebService interface for information

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.