Search code examples
phpandroidweb-serviceswsdlksoap2

ksoap2 response is null. responseDump is the wsdl file itself, not an envelope


So I have a PHP web-service which I want to access from an Android app through SOAP. I'm positive that the web-service is working as it should because I built a PHP client locally and tried to request from the web-service (which is online), and the PHP client was able to retrieve the correct data. The problem is a NullPointerException when I try the "getResponse()" of the envelope. Also, the envelope's "bodyIn" is also null. I tried and outputted the responseDump of the HttpTransportSE and it showed the WSDL file itself. Shouldn't it return a response envelope, not the WSDL file?

The Android Client:

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

    }

}

The WSDL file (which is also the same thing I get when I print the 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>

The PHP web-service:

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

?>

Also, I've read some solutions that the URL should point to the PHP web service instead of the wsdl file. I tried it and it was still null. Although when I checked the responseDump, all I see were special characters.


Solution

  • I fixed my own problem! So it turns out that other people are correct, when accessing a PHP web-service, the URL must point to the web-service itself, not the WSDL file as some guides have mentioned. Now I have already tried pointing it to the PHP web-service, but the error still remains, which it turns out was caused by a ClassCastException because the response was an array of integers, so instead of the simple:

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

    What must be done is:

    Vector<SoapObject> response = (Vector<SoapObject>)envelope.getResponse();