运行时错误使用Android KSOAP进行getResponse时

时间:2011-05-05 00:13:16

标签: android web-services axis2 ksoap2 android-ksoap2

我的Android在SoapObject中获得运行时错误结果=(SoapObject)envelope.getResponse(); 网络服务在我的笔记本电脑(192.168.15.13),笔记本电脑和Android通过LAN连接。我在Knopflerfish OSGi中使用Axis2创建了Web服务。这是我的代码:

package test.msani.aksessoap;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
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.Bundle;
import android.util.Log;
import android.widget.TextView;

public class TesAksesSOAP extends Activity {
    private static final String SOAP_ACTION = "SOAPObjectSani.SOAPObjectSani/SOAPObjectSani/getKeterangan";
    private static final String METHOD_NAME = "getKeterangan";
    private static final String NAMESPACE = "SOAPObjectSani.SOAPObjectSani/SOAPObjectSani";
    private static final String URL = "http://192.168.15.13:8080/axis2/services/SOAPObjectSani";
    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        //set parameter
        PropertyInfo pi = new PropertyInfo();
        pi.setName("angka");
        pi.setValue(2);
        pi.setType(int.class);
        request.addProperty(pi);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        Log.i("Sani", "coming 1");

        try {
            Log.i("Sani", "coming 2");
            androidHttpTransport.call(SOAP_ACTION, envelope);
            Log.i("Sani", "coming 3");
        } catch (IOException e) {
            tv.setText("IOException");
        } catch (XmlPullParserException e) {
            tv.setText("XmlPullParserException");
        }

        SoapObject result = null;
        try {
            Log.i("Sani", "coming 4");
            result = (SoapObject)envelope.getResponse(); //I get the error here
            Log.i("Sani", "coming 5");
        } catch (SoapFault e) {
            tv.setText("SoapFault");
        }

        //to get the data
        String resultData = result.getProperty(0).toString();

        tv.setText("" + resultData);

        setContentView(tv);
    }
}

这是WSDL

<?xml version="1.0" encoding="UTF-8"?><wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ns0="SOAPObjectSani.SOAPObjectSani/SOAPObjectSani" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="SOAPObjectSani.SOAPObjectSani/SOAPObjectSani">
    <wsdl:types>
        <xs:schema xmlns:ns="SOAPObjectSani.SOAPObjectSani/SOAPObjectSani" attributeFormDefault="qualified" elementFormDefault="unqualified" targetNamespace="SOAPObjectSani.SOAPObjectSani/SOAPObjectSani">
            <xs:element name="getTanggalSpesial">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="peristiwa" nillable="true" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="getTanggalSpesialResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element maxOccurs="unbounded" minOccurs="0" name="return" type="xs:int"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="getKeterangan">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="angka" type="xs:int"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="getKeteranganResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>
    </wsdl:types>
    <wsdl:message name="getKeteranganRequest">
        <wsdl:part name="parameters" element="ns0:getKeterangan"/>
    </wsdl:message>
    <wsdl:message name="getKeteranganResponse">
        <wsdl:part name="parameters" element="ns0:getKeteranganResponse"/>
    </wsdl:message>
    <wsdl:message name="getTanggalSpesialRequest">
        <wsdl:part name="parameters" element="ns0:getTanggalSpesial"/>
    </wsdl:message>
    <wsdl:message name="getTanggalSpesialResponse">
        <wsdl:part name="parameters" element="ns0:getTanggalSpesialResponse"/>
    </wsdl:message>
    <wsdl:portType name="SOAPObjectSaniPortType">
        <wsdl:operation name="getKeterangan">
            <wsdl:input message="ns0:getKeteranganRequest" wsaw:Action="urn:getKeterangan"/>
            <wsdl:output message="ns0:getKeteranganResponse" wsaw:Action="urn:getKeteranganResponse"/>
        </wsdl:operation>
        <wsdl:operation name="getTanggalSpesial">
            <wsdl:input message="ns0:getTanggalSpesialRequest" wsaw:Action="urn:getTanggalSpesial"/>
            <wsdl:output message="ns0:getTanggalSpesialResponse" wsaw:Action="urn:getTanggalSpesialResponse"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="SOAPObjectSaniSOAP11Binding" type="ns0:SOAPObjectSaniPortType">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="getKeterangan">
            <soap:operation soapAction="urn:getKeterangan" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="getTanggalSpesial">
            <soap:operation soapAction="urn:getTanggalSpesial" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="SOAPObjectSaniSOAP12Binding" type="ns0:SOAPObjectSaniPortType">
        <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="getKeterangan">
            <soap12:operation soapAction="urn:getKeterangan" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="getTanggalSpesial">
            <soap12:operation soapAction="urn:getTanggalSpesial" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="SOAPObjectSaniHttpBinding" type="ns0:SOAPObjectSaniPortType">
        <http:binding verb="POST"/>
        <wsdl:operation name="getKeterangan">
            <http:operation location="SOAPObjectSani/getKeterangan"/>
            <wsdl:input>
                <mime:content type="text/xml" part="getKeterangan"/>
            </wsdl:input>
            <wsdl:output>
                <mime:content type="text/xml" part="getKeterangan"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="getTanggalSpesial">
            <http:operation location="SOAPObjectSani/getTanggalSpesial"/>
            <wsdl:input>
                <mime:content type="text/xml" part="getTanggalSpesial"/>
            </wsdl:input>
            <wsdl:output>
                <mime:content type="text/xml" part="getTanggalSpesial"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="SOAPObjectSani">
        <wsdl:port name="SOAPObjectSaniSOAP11port_http" binding="ns0:SOAPObjectSaniSOAP11Binding">
            <soap:address location="http://0.0.0.0:8080/axis2/services/SOAPObjectSani"/>
        </wsdl:port>
        <wsdl:port name="SOAPObjectSaniSOAP12port_http" binding="ns0:SOAPObjectSaniSOAP12Binding">
            <soap12:address location="http://0.0.0.0:8080/axis2/services/SOAPObjectSani"/>
        </wsdl:port>
        <wsdl:port name="SOAPObjectSaniHttpport" binding="ns0:SOAPObjectSaniHttpBinding">
            <http:address location="http://0.0.0.0:8080/axis2/services/SOAPObjectSani"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>
请帮助我......

1 个答案:

答案 0 :(得分:4)

使用此行

 response = (SoapObject) envelope.bodyIn;

而不是

result = (SoapObject)envelope.getResponse();