試試這個代碼。我在模擬器上成功運行它。
予取源代碼從與一些變化: http://egkatzioura.wordpress.com/2011/07/04/nusoap-and-ksoap2-android-and-php-alliance/
Server.php
<?php
// include NuSOAP library
require_once('nusoap-0.9.5/lib/nusoap.php');
// Create Web Service Server
$server = new nusoap_server;
$server->configureWSDL('aritmatikawsdl', 'urn:aritmatikawsdl');
$server->register('pollServer',
array('value' => 'xsd:string'),
array('return' => 'xsd:string'),
'urn:aritmatikawsdl',
'urn:aritmatikawsdl#pollServer',
'rpc',
'encoded',
'Test'
);
// Define Services
function pollServer($value){
if($value['value'] == 'Good'){
return $value['value'].""." The value of the server poll resulted in good information";
}
else{
return $value['value'].""." The value of the server poll showed poor information";
}
}
$server->service($HTTP_RAW_POST_DATA);
?>
KSOAP2
package org.tuxpan;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Adder extends Activity{
private static final String SOAP_ACTION = "pollServer";
private static final String METHOD_NAME = "pollServer";
private static final String NAMESPACE = "http://10.0.2.2/DroidAssessment/server.php";
private static final String URL = "http://10.0.2.2/DroidAssessment/server.php?wsdl";
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
SoapObject soapclient = new SoapObject(NAMESPACE,METHOD_NAME);
//Yes you need this one in order to send the whole string or else only the first letter
//is going to be send
SoapObject parameters = new SoapObject(NAMESPACE, METHOD_NAME);
parameters.addProperty("value","Good");
soapclient.addProperty(METHOD_NAME,parameters);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(soapclient);
HttpTransportSE httpTransportSE = new HttpTransportSE(URL);
try {
httpTransportSE.call(SOAP_ACTION, envelope);
Log.v("TEST","runs ok attributes "+ envelope.getResponse().toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v("TEST","io wrong");
} catch (XmlPullParserException e) {
Log.v("TEST","xml wrong");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}