我正在開發一個使用基於php的web服務的android應用程序。我知道有很多這方面的問題,但在三天的閱讀後,我可以說答案對我沒有幫助。XmlPullParserException在Android中使用Ksoap2消費php web服務
通過一方面我有PHP服務,看起來像這樣:
服務器端樹文件夾:
http://IP/WSexample/wsdl/
- hello_server.php
- hello.wsdl
PHP部分(hello_server.php):
if (!extension_loaded("soap")) {
dl("php_soap.dll");
}
ini_set("soap.wsdl_cache_enabled", "0");
$server = new SoapServer("hello.wsdl");
function sayHello($yourName = '') {
if (empty($yourName)) $yourName = "Mundo";
return "Hello, ".$yourName;
}
$server->addFunction("sayHello");
$server->handle();
XML部分(hello.wsdl):
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="HelloService"
targetNamespace="http://XX.XX.XX.XX/WSexample/wsdl/hello.wsdl"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://XX.XX.XX.XX/WSexample/wsdl/hello.wsdl"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<message name="SayHelloRequest">
<part name="firstName" type="xsd:string"/>
</message>
<message name="SayHelloResponse">
<part name="greeting" type="xsd:string"/>
</message>
<portType name="Hello_PortType">
<operation name="sayHello">
<input message="tns:SayHelloRequest"/>
<output message="tns:SayHelloResponse"/>
</operation>
</portType>
<binding name="Hello_Binding" type="tns:Hello_PortType">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="sayHello">
<soap:operation soapAction="sayHello"/>
<input>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:examples:helloservice"
use="encoded"/>
</input>
<output>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:examples:helloservice"
use="encoded"/>
</output>
</operation>
</binding>
<service name="Hello_Service">
<documentation>WSDL File for HelloService</documentation>
<port binding="tns:Hello_Binding" name="Hello_Port">
<soap:address
location="http://XX.XX.XX.XX/WSexample/wsdl/hello_server.php"/>
</port>
</service>
</definitions>
如果我試圖用我自己的PHP客戶端,服務器工作正常,返回您好,我給它的名字,即使我嘗試用http://validwsdl.com,它得到正確的數據... Android是我的問題...
我只是有一個簡單的Activity來獲取數據,但是當我使用HttpTransportSE對象的方法調用時,它總是給我一個XmlPullParserException。異常說:
org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <definitions name='HelloService' targetNamespace='http://XX.XX.XX.XX/WSexample/wsdl/hello.wsdl'>@7:49 in [email protected])
的Android代碼:
public class MainActivity extends Activity {
public static final String NAMESPACE="urn:examples:helloservice";
public static final String METHOD_NAME ="sayHello";
public static final String URL = "http://XX.XX.XX.XX/WSexample/wsdl/hello.wsdl";
public static final String SOAP_ACTION = "sayHello";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.tv);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("firstName", "John");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
try {
ht.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
tv.setText("Mensaje: "+response.toString());
} catch (XmlPullParserException i){
Log.e("MI_ERROR", i.getMessage());
}catch(Exception e) {
Log.e("MI_ERROR", e.getMessage());
e.printStackTrace();
}
}
}
我覺得我有事情做與屬性:URL,命名空間METHOD_NAME和SOAP_ACTION,我已經試過更改爲其他選項,但沒有成功。
當我改變URL屬性:
public static final String URL = "http://XX.XX.XX.XX/WSexample/wsdl/hello_server.php";
異常變化:
org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT [email protected]:0 in [email protected])
請幫助!