1. Create Activity.
package com.web; 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.os.Handler; import android.os.Message; import android.os.StrictMode; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class WebServiceTestActivity extends Activity implements OnClickListener { private EditText et_param01; private EditText et_param02; private EditText et_param03; private Button btn_submit; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et_param01 = (EditText) findViewById(R.id.et_param01); et_param02 = (EditText) findViewById(R.id.et_param02); et_param03 = (EditText) findViewById(R.id.et_param03); btn_submit = (Button) findViewById(R.id.btn_submit); btn_submit.setOnClickListener(this); /** * 防止android.os.NetworkOnMainThreadException异常 * 参考http://dev.wo.com.cn/docportal/doc_queryMdocDetail.action?mdoc.docindex=6695 */ StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads().detectDiskWrites().detectNetwork() .penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects().detectLeakedClosableObjects() .penaltyLog().penaltyDeath().build()); } public String callService(String param1, String param2) { try { // 命名空间 String nameSpace = "http://10.8.60.63:8088/webservice/services/webCallService/"; // 调用的方法名称 String methodName = "callService"; // SOAP Action String soapAction = nameSpace + methodName; SoapObject request = new SoapObject(nameSpace, methodName); request.addProperty("param1", param1); request.addProperty("param2", param2); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = true; envelope.bodyOut = request; envelope.setOutputSoapObject(request); envelope.encodingStyle="UTF-8"; HttpTransportSE transport = new HttpTransportSE( "http://10.8.60.63:8088/webservice/services/webCallService");// wsdl文档 try { // 调用WebService transport.call(soapAction, envelope); // transport.call(null, envelope); } catch (Exception e) { e.printStackTrace(); } Object obj = envelope.getResponse(); return obj.toString(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace (); return null ; } } @Override public void the onClick (View V) { Switch (v.getId ()) { Case R.id.btn_submit: new new the Thread () { @Override public void RUN () { // the way you want to perform // after the implementation to send a null message handler handler.sendEmptyMessage (0 ); } } .start (); BREAK ; } } // define Handler object Private Handler Handler = new new Handler () { @Override // when there is a message sent out on the implementation of this method Handler public void the handleMessage (the Message MSG) { // process the UI String STR = (String) callService ( et_param01.getText () toString (), et_param02.getText (). .toString ()); IF (STR == null || "" .equals (STR)) { STR = "return value is null" ; } et_param03 .setText (STR); } }; }
2. Configure the manifest file.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.web" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="15" /> <!-- 访问网络的权限 --> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".WebServiceTestActivity" 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> </manifest>
3. Test results.
Reproduced in: https: //www.cnblogs.com/FCWORLD/p/3491031.html