来源:http://www.fkblog.org/blog59
QQ:429240967
转载请标明出处
下载ksoap2:http://code.google.com/p/ksoap2-android/downloads/list
下载nusoap:http://sourceforge.net/projects/nusoap/
转载请标明出处
新建Android工程TestSoap导入ksoap
设置AndroidMainfest.xml文件参数添加:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
服务端Nusoap代码service.php <?php // Pull in the NuSOAP code require_once('nusoap.php'); // Create the server instance $server = new soap_server(); $server->xml_encoding = 'utf-8'; $server->soap_defencoding = 'utf-8'; // Initialize WSDL support $server->configureWSDL('hellowsdl', 'urn:hello'); // Put the WSDL schema types in the namespace with the tns prefix $server->wsdl->schemaTargetNamespace = 'urn:hello'; // Register the method to expose $server->register('hello', // method name array('name' => 'xsd:string'), // input parameters array('return' => 'xsd:string'), // output parameters 'urn:hello', // namespace 'urn:hello#hello', // soapaction 'rpc', // style 'encoded', // use 'Says hello to the caller' // documentation ); // Define the method as a PHP function function hello($name) { $str = 'Hello'.${name}; return $str; } // Use the request to (try to) invoke the service $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; $server->service($HTTP_RAW_POST_DATA); ?> Android-Ksoap2 客户端代码 package com.test.soap; 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.TextView; public class TestSoap extends Activity { TextView v = null; String hello2 = null; private static final String METHOD_NAME = "hello"; private static final String NAMESPACE = "urn:hello"; private static final String URL = "http://192.168.1.245/cobubService/service2.php?wsdl"; private static final String SOAP_ACTION = "urn:hello#hello"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("name", "Android"); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); try { androidHttpTransport.call(SOAP_ACTION, envelope); SoapObject resultRequestSOAP = (SoapObject) envelope.bodyIn; v=(TextView)findViewById(R.id.textView1); v.setText(resultRequestSOAP.getProperty("return").toString()); } catch(Exception e) { e.printStackTrace(); } } }