Access Webservice from Android
Supported Operating Systems
Supported Development EnvironmentsEclipse IDE
Caution: There are known issues with the ADT plugin running with Eclipse 3.6. Please stay on 3.5 until further notice.
Hardware RequirementsThe Android SDK requires disk storage for all of the components that you choose to install. The table below provides a rough idea of the disk-space requirements to expect,based on the components that you plan to use.
Note that the disk-space requirements above arein addition to those of the Eclipse IDE,JDK,or other prerequisite tools that you may need to install on your development computer. Installing the ADT Plugin for Eclipse
In the "Location" field,enter this URL: https://dl-ssl.google.com/android/eclipse/ Note: If you have trouble acquiring the plugin,you can try using "http" in the URL,instead of "https" (https is preferred for security reasons). Click OK.
Configuring the ADT PluginOnce you've successfully downloaded ADT as described above,the next step is to modify your ADT preferences in Eclipse to point to the Android SDK directory:
Click Apply,then OK. Creating an Android ProjectThe ADT plugin provides a New Project Wizard that you can use to quickly create a new Android project (or a project from existing code). To create a new project:
Tip: You can also start the New Project Wizard from theNew icon in the toolbar. Once you complete the New Project Wizard,ADT creates the following folders and files in your new project:
Creating an AVDAn Android Virtual Device (AVD) is a device configuration for the emulator that allows you to model real world devices. In order to run an instance of the emulator,you must create an AVD. To create an AVD from Eclipse:
Your AVD is now ready and you can either close the SDK and AVD Manager,create more AVDs,or launch an emulator with the AVD by selecting a device and clickingStart. Code your FirstAppUI.java class like below:
package com.test.android; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class FirstAppUI extends Activity { private static final String NAMESPACE = "com.service.ServiceImpl"; private static final String URL = "http://192.168.202.124:9000/AndroidWS/wsdl/ServiceImpl.wsdl"; private static final String SOAP_ACTION = "ServiceImpl"; private static final String METHOD_NAME = "message"; private static final String[] sampleACTV = new String[] { "android","iphone","blackberry" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayAdapter<String> arrAdapter = new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,sampleACTV); AutoCompleteTextView ACTV = (AutoCompleteTextView)findViewById (R.id.AutoCompleteTextView01); ACTV.setAdapter(arrAdapter); SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); try { androidHttpTransport.call(SOAP_ACTION,envelope); SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn; ACTV.setHint("Received :" + resultsRequestSOAP.toString()); } catch (Exception e) { e.printStackTrace(); } } }Insert AutoCompleteTextView to your res>layout >
main.xml as:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <AutoCompleteTextView android:id="@+id/AutoCompleteTextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="This is Hint" android:width="240px" /> </LinearLayout>Insert Internet access permission to your mobile application in AndroidManifest.xml file as:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.android" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".FirstAppUI" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-sdk android:minSdkVersion="8" /> </manifest>ServiceImpl.wsdl:
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions targetNamespace=http://service.com xmlns:apachesoap=http://xml.apache.org/xml-soap xmlns:impl="http://service.com" xmlns:intf=http://service.com xmlns:wsdl=http://schemas.xmlsoap.org/wsdl/ xmlns:wsdlsoap=http://schemas.xmlsoap.org/wsdl/soap/ xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!--WSDL created by Apache Axis version: 1.4 Built on Apr 22,2006 (06:55:48 PDT)--> <wsdl:types> <schema elementFormDefault="qualified" targetNamespace="http://service.com" xmlns="http://www.w3.org/2001/XMLSchema"> <element name="message"> <complexType/> </element> <element name="messageResponse"> <complexType> <sequence> <element name="messageReturn" type="xsd:string"/> </sequence> </complexType> </element> </schema> </wsdl:types> <wsdl:message name="messageResponse"> <wsdl:part element="impl:messageResponse" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="messageRequest"> <wsdl:part element="impl:message" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:portType name="ServiceImpl"> <wsdl:operation name="message"> <wsdl:input message="impl:messageRequest" name="messageRequest"> </wsdl:input> <wsdl:output message="impl:messageResponse" name="messageResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="ServiceImplSoapBinding" type="impl:ServiceImpl"> <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="message"> <wsdlsoap:operation soapAction=""/> <wsdl:input name="messageRequest"> <wsdlsoap:body use="literal"/> </wsdl:input> <wsdl:output name="messageResponse"> <wsdlsoap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="ServiceImplService"> <wsdl:port binding="impl:ServiceImplSoapBinding" name="ServiceImpl"> <wsdlsoap:address location= "http://localhost:9000/AndroidWS/services/ServiceImpl"/> </wsdl:port> </wsdl:service> </wsdl:definitions> This is from : http://www.codeproject.com/Articles/112381/Step-by-Step-Method-to-Access-Webservice-from-Andr 推荐:http://www.c-sharpcorner.com/UploadFile/88b6e5/how-to-call-web-service-in-android-using-soap/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |