douzhan1238 2013-04-14 14:43
浏览 64
已采纳

XmlPullParserException在android中使用Ksoap2的php web服务

I'm developing an android application that consumes a php based web service. I know there's so many questions about this, but after three days reading I can say the answers didn't help me.

By one hand I've got the php service that looks like this:

Server Side tree folder:

  http://IP/WSexample/wsdl/
           - hello_server.php
           - hello.wsdl

PHP part (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 part (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>

If I try with my own php client, the server works fine and return Hello and the name I give to it, even if I try with http://validwsdl.com, it gets the proper data...Android is my problem...

I just have a simple Activity to fetch the data, but it always give me a XmlPullParserException when I use the method call of the HttpTransportSE object. The exception says:

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) 

Android Code:

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();
        }

    }
}

I think I have something to do with attributes: URL, NAMESPACE, METHOD_NAME and SOAP_ACTION, I've tried changing them to other options but no success.

When I change URL attribute to:

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

the exception change to:

org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:0 in java.io.InputStreamReader@40570008) 

Please help!!!

  • 写回答

1条回答 默认 最新

  • dongshi2141 2013-05-14 20:05
    关注

    Finally the fault was in string URL. I have to put the second one:

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

    And the XMLPullParserException was a server coding error.

    One very useful tool to know if WSDL is working is this web:

    http://www.validwsdl.com/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器