Android connects to webservice (using the jar package provided by Google) and androidwebservice

Source: Internet
Author: User

Android connects to webservice (using the jar package provided by Google) and androidwebservice

Android development, need to connect webservice, before you want to use Google provided jar package: http://pan.baidu.com/s/1hqMTUHe

Download it and paste it to the libs Folder:

There are many similar methods on the Internet. I tried a lot but failed. I finally found that the jar package I downloaded was faulty, which caused me to stay stuck.

First, add the network permission:

<uses-permission android:name="android.permission.INTERNET"/>

This is all the code, including obtaining data from the remote webservice and displaying it with ListView:

Package com. example. webservice; import java. io. IOException; import java. util. arrayList; import java. util. list; import org. ksoap2.SoapEnvelope; import org. ksoap2.SoapFault; import org. ksoap2.serialization. soapObject; import org. ksoap2.serialization. soapSerializationEnvelope; import org. ksoap2.transport. httpTransportSE; import org. xmlpull. v1.XmlPullParserException; import android. OS. bundle; import android. app. A Ctivity; import android. view. menu; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. arrayAdapter; import android. widget. listAdapter; import android. widget. listView; import android. widget. toast; public class MainActivity extends Activity {// nameSpace String nameSpace = "http://tempuri.org/"; // call method String methodName = "selectSt UAll "; // method name for webservice: String endPoint =" http: // 10.0.2.2: 8011/WebService. asmx? Wsdl "; // link to local test complete String soapAction =" http://tempuri.org/selectStuAll "; // namespace and call method name private ListView listview; private ListAdapter adapter; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listview = (ListView) findViewById (R. id. listView1); adapter = new ArrayAdapter <String> (MainActivity. this, android. r. layout. simple_list_ite M_1, getsource (); listview. setAdapter (adapter); listview. setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {// TODO automatically generated method stub Toast. makeText (MainActivity. this, ">" + adapter. getItem (arg2), 1 ). show () ;}}) ;}public List <String> getsource () {List <String> list = new ArrayList <String> (); // instantiate SoapObject object SoapObject request = new SoapObject (nameSpace, methodName); // Step 2: Set the call method parameter/* request. addProperty ("username", "1"); request. addProperty ("password", "2"); request. addProperty ("tel", "3"); * // obtain the serialized Envelope SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope. VER12); envelope. bodyOut = request; envelope. dotNet = true; HttpTransportSE transport = new HttpTransportSE (endPoint); try {transport. call (soapAction, envelope);} catch (IOException e) {// catch Block e automatically generated by TODO. printStackTrace ();} catch (XmlPullParserException e) {// catch Block e automatically generated by TODO. printStackTrace ();} SoapObject result = null; try {result = (SoapObject) envelope. getResponse ();} catch (SoapFault e) {// catch Block e automatically generated by TODO. printStackTrace ();} int count = result. getPropertyCount (); for (int index = 1; index <count; index = index + 3) {list. add (result. getProperty (index ). toString () ;}return list ;}}

In this case, I will help you solve the problem if there is a problem with this method.

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.