【问题标题】:XmlPullParserException consuming a php web service with Ksoap2 in androidXmlPullParserException 在 android 中使用带有 Ksoap2 的 php Web 服务
【发布时间】:2013-04-14 14:43:13
【问题描述】:

我正在开发一个使用基于 php 的 Web 服务的 android 应用程序。我知道对此有很多问题,但经过三天的阅读,我可以说答案对我没有帮助。

一方面,我得到了如下所示的 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 客户端,服务器可以正常工作并返回 Hello 和我给它的名称,即使我尝试使用 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 java.io.InputStreamReader@405701b0) 

安卓代码:

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、NAMESPACE、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 null@1:0 in java.io.InputStreamReader@40570008) 

请帮忙!!!

【问题讨论】:

    标签: php android web-services


    【解决方案1】:

    最后,问题出在字符串 URL 中。 我必须放第二个:

    public static final String URL = "http://XX.XX.XX.XX/WSexample/wsdl/hello_server.php";
    

    XMLPullParserException 是服务器编码错误。

    了解 WSDL 是否正常工作的一个非常有用的工具是这个网站:

    http://www.validwsdl.com/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多