1

所以我有一个 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 时,我看到的只是特殊字符。

4

2 回答 2

1

我解决了我自己的问题!所以事实证明其他人是正确的,当访问 PHP Web 服务时,URL 必须指向 Web 服务本身,而不是一些指南提到的 WSDL 文件。现在我已经尝试将它指向 PHP Web 服务,但错误仍然存​​在,事实证明这是由 ClassCastException 引起的,因为响应是一个整数数组,所以不是简单的:

SoapObject response = (SoapObject)envelope.getResponse();

必须做的是:

Vector<SoapObject> response = (Vector<SoapObject>)envelope.getResponse();
于 2012-09-25T10:26:31.430 回答
1

对于那些因为您的例外而到达这里的人:

Log.e("responseDump", transport.responseDump.toString());

或您的:

System.out.println(transport.responseDump.toString());

在打印字符串之前,您必须确保您的调用结构如下:

HttpTransportSE transport = new HttpTransportSE(URL);
transport.debug = true;
transport.call(SOAP_ACTION, envelope);

如果你有你的transport.debug = true; transport.call(SOAP_ACTION,信封)之后;,你会崩溃,你的弦会燃烧!确保在调用之前将 debug 设置为 true ...

于 2013-04-04T00:25:56.803 回答