我正在使用 ksoap2 为 android 开发 Web 服务。我正在编写的代码返回一个错误(xml exeption),我不知道错误在哪里,我认为它可能是操作或 url。我一直在寻找一个完整的 php web 服务示例来处理但没有用
这是 server.php 文件
<?php
// Pull in the NuSOAP code
require_once("lib/nusoap.php");
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
$server->configureWSDL('server', 'urn:server');
// Put the WSDL schema types in the namespace with the tns prefix
$server->wsdl->schemaTargetNamespace = 'urn:server';
// Register the method to expose
$server->register('pollServer',
// method name
array('value' => 'xsd:string'),
// input parameters
array('return' => 'xsd:string'),
// output parameters
'urn:server',
// namespace
'urn:server#pollServer',
// soapaction
'rpc',
// style
'encoded',
// use
'Says hello to the caller'
// documentation
);
// Define the method as a PHP function
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";
}
}
// 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);
?>
这是客户端 java 文件:
package com.restaurantApp;
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.util.Log;
public class Example {
private static final String SOAP_ACTION = "urn:server#pollServer";
private static final String METHOD_NAME = "pollServer";
private static final String NAMESPACE = "urn:server";
private static final String URL = "http://sara-alabbasi.com/server.php";
public Example(){
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();
}
}
}