所以我有一个 PHP 网络服务,我想通过 SOAP 从 Android 应用程序访问它。我很肯定网络服务正在正常工作,因为我在本地构建了一个 PHP 客户端并尝试从网络服务(在线)请求,并且 PHP 客户端能够检索正确的数据。当我尝试信封的“getResponse()”时,问题是 NullPointerException。此外,信封的“bodyIn”也为空。我尝试并输出了 HttpTransportSE 的 responseDump,它显示了 WSDL 文件本身。它不应该返回响应信封,而不是 WSDL 文件吗?
安卓客户端:
package com.franzsarmiento.shorecloudsurvey;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
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.AsyncTask;
import android.os.Bundle;
public class MainMenu extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
new DownloadNewSurveys().execute();
}
private class DownloadNewSurveys extends AsyncTask<Void,Void,Void> {
private final static String NAMESPACE = "http://203.177.73.175:8080/";
private final static String METHOD_NAME = "getSid";
private final static String SOAP_ACTION = "http://203.177.73.175:8080/getSid";
private final static String URL = "http://203.177.73.175:8080/sid.wsdl";
@Override
protected Void doInBackground(Void... arg0) {
SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
SoapSerializationEnvelope envelope = new
SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.debug = true;
try {
ht.call(SOAP_ACTION, envelope);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
SoapObject response = null;
System.out.println(ht.responseDump);
try {
response = (SoapObject)envelope.getResponse();
} catch (SoapFault e) {
response = (SoapObject)envelope.bodyIn;
}
System.out.println(response);
return null;
}
}
}
WSDL 文件(这也是我打印 responseDump 时得到的相同内容):
<definitions name='Sid'
targetNamespace='http://203.177.73.175:8080/'
xmlns:tns='http://203.177.73.175:8080/'
xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
xmlns='http://schemas.xmlsoap.org/wsdl/'>
<message name='getSidRequest'/>
<message name='getSidResponse'>
<part name='sidArray' type='tns:ArrayOfinteger[]'/>
</message>
<portType name='SidPortType'>
<operation name='getSid'>
<input message='tns:getSidRequest'/>
<output message='tns:getSidResponse'/>
</operation>
</portType>
<binding name='SidBinding' type='tns:SidPortType'>
<soap:binding style='rpc'
transport='http://schemas.xmlsoap.org/soap/http'/>
<operation name='getSid'>
<soap:operation soapAction='getSid'/>
<input>
<soap:body use='encoded' namespace='urn:sid'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</input>
<output>
<soap:body use='encoded' namespace='urn:sid'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</output>
</operation>
</binding>
<service name='SidService'>
<port name='SidPort' binding='SidBinding'>
<soap:address location='http://203.177.73.175:8080/sid_server.php'/>
</port>
</service>
</definitions>
PHP 网络服务:
<?php
function getSid() {
$con = mysql_connect('localhost','fsarmiento','shorecloud') or die (mysql_error());
$db = mysql_select_db('limesurvey',$con);
$query = '
SELECT
sid
FROM
lime_surveys
;';
$result = mysql_query($query) or die (mysql_error());
$arrSid = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$arrSid[] = intval($row['sid']);
}
return $arrSid;
}
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$server = new SoapServer("sid.wsdl", array('encoding'=>'UTF-8'));
$server->addFunction("getSid");
$server->handle();
?>
此外,我还阅读了一些解决方案,即 URL 应指向 PHP Web 服务而不是 wsdl 文件。我试过了,它仍然是空的。虽然当我检查 responseDump 时,我看到的只是特殊字符。