使用 Android KSOAP getResponse 时出现运行时错误
我的 Android 在 SoapObject result = (SoapObject)envelope.getResponse(); 中出现运行时错误 Web服务在我的笔记本(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>
请帮助我...
My Android get a runtime error in SoapObject result = (SoapObject) envelope.getResponse();
The webservice is in my notebook (192.168.15.13), the notebook and the Android connected via LAN. I created the web service using Axis2 in Knopflerfish OSGi. Here is my code:
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);
}
}
and here is the 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>
help me please...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用这一行
代替
Use this line
Instead of