我正在做一个移动应用程序,通过使用ksoap2使用Web服务。
到目前为止,我能够通过包含String,double,int等的web服务发送复杂对象......
然后我能够通过网络发送一个字节数组。现在出现了我的问题:
当我尝试创建一个通过网络发送的对象,当其中一个参数是一个字节数组时,我从服务器获得一个faulttring。
我正在访问的网络服务有以下wsdl文件:
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://sensors.components" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ax21="http://sensors.components/xsd" 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="http://sensors.components">
<wsdl:types>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://sensors.components/xsd">
<xs:complexType name="Pic">
<xs:sequence>
<xs:element minOccurs="0" name="accuracy" type="xs:double"/>
<xs:element minOccurs="0" name="imageInByte" nillable="true" type="xs:base64Binary"/>
<xs:element minOccurs="0" name="latitude" type="xs:double"/>
<xs:element minOccurs="0" name="longitude" type="xs:double"/>
<xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="time" type="xs:long"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
<xs:schema xmlns:ax22="http://sensors.components/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://sensors.components">
<xs:import namespace="http://sensors.components/xsd"/>
<xs:element name="pictureWebservice">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="args0" nillable="true" type="ax21:Pic"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="pictureWebserviceResponse">
<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="pictureWebserviceRequest">
<wsdl:part name="parameters" element="ns:pictureWebservice"/>
</wsdl:message>
<wsdl:message name="pictureWebserviceResponse">
<wsdl:part name="parameters" element="ns:pictureWebserviceResponse"/>
</wsdl:message>
<wsdl:portType name="PictureWSPortType">
<wsdl:operation name="pictureWebservice">
<wsdl:input message="ns:pictureWebserviceRequest" wsaw:Action="urn:pictureWebservice"/>
<wsdl:output message="ns:pictureWebserviceResponse" wsaw:Action="urn:pictureWebserviceResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="PictureWSSoap11Binding" type="ns:PictureWSPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="pictureWebservice">
<soap:operation soapAction="urn:pictureWebservice" 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="PictureWSSoap12Binding" type="ns:PictureWSPortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="pictureWebservice">
<soap12:operation soapAction="urn:pictureWebservice" 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="PictureWSHttpBinding" type="ns:PictureWSPortType">
<http:binding verb="POST"/>
<wsdl:operation name="pictureWebservice">
<http:operation location="pictureWebservice"/>
<wsdl:input>
<mime:content type="application/xml" part="parameters"/>
</wsdl:input>
<wsdl:output>
<mime:content type="application/xml" part="parameters"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="PictureWS">
<wsdl:port name="PictureWSHttpSoap11Endpoint" binding="ns:PictureWSSoap11Binding">
<soap:address location="http://accessible-serv.lasige.di.fc.ul.pt:8181/axis2/services/PictureWS.PictureWSHttpSoap11Endpoint/"/>
</wsdl:port>
<wsdl:port name="PictureWSHttpSoap12Endpoint" binding="ns:PictureWSSoap12Binding">
<soap12:address location="http://accessible-serv.lasige.di.fc.ul.pt:8181/axis2/services/PictureWS.PictureWSHttpSoap12Endpoint/"/>
</wsdl:port>
<wsdl:port name="PictureWSHttpEndpoint" binding="ns:PictureWSHttpBinding">
<http:address location="http://accessible-serv.lasige.di.fc.ul.pt:8181/axis2/services/PictureWS.PictureWSHttpEndpoint/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
我要发送的对象如下:
public class Pic implements KvmSerializable{
private double latitude;
private double longitude;
private long time;
private double accuracy;
private String name;
private byte[] imageInByte;
public byte[] getImageInByte() {
return imageInByte;
}
public void setImageInByte(byte[] imageInByte) {
this.imageInByte = imageInByte;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public double getAccuracy() {
return accuracy;
}
public void setAccuracy(double accuracy) {
this.accuracy = accuracy;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
@Override
public Object getProperty(int arg0) {
switch(arg0){
case 0:
return latitude;
case 1:
return longitude;
case 2:
return time;
case 3:
return accuracy;
case 4:
return name;
case 5:
return imageInByte;
}
return null;
}
@Override
public int getPropertyCount() {
return 6;
}
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
switch(arg0){
case 0:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "latitude";
break;
case 1:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "longitude";
break;
case 2:
arg2.type = PropertyInfo.LONG_CLASS;
arg2.name = "time";
break;
case 3:
arg2.type = Double.class;
arg2.name = "accuracy";
break;
case 4:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "name";
break;
case 5:
arg2.type = MarshalBase64.BYTE_ARRAY_CLASS;
arg2.name = "imageInBytes";
default:
break;
}
}
@Override
public void setProperty(int arg0, Object arg1) {
// TODO Auto-generated method stub
switch(arg0){
case 0:
latitude = Double.parseDouble(arg1.toString());
break;
case 1:
longitude = Double.parseDouble(arg1.toString());
break;
case 2:
time = Long.parseLong(arg1.toString());
break;
case 3:
accuracy = Double.parseDouble(arg1.toString());
break;
case 4:
name = arg1.toString();
break;
case 5:
imageInByte = (byte[])arg1;
default:
break;
}
}
我用来访问网络服务的价值如下:
namespace =“http://sensors.components” url =“http://accessible-serv.lasige.di.fc.ul.pt:8181/axis2/services/PictureWS?wsdl” soap action =“http://sensors.components/pictureWebservice” method name =“pictureWebservice”
然后我用来将它发送到网络服务的代码如下:
String NAMESPACE = "http://sensors.components/xsd";
try {
SoapObject request = new SoapObject(connection.getNAMESPACE(), connection.getMETHOD_NAME());
PropertyInfo object = new PropertyInfo();
object.setName("picture");
object.setValue(picture);
object.setType(picture.getClass());
object.setNamespace(NAMESPACE);
request.addProperty(object);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
MarshalDouble md = new MarshalDouble();
md.register(envelope);
new MarshalBase64().register(envelope);
envelope.addMapping(NAMESPACE, "Pic", new Pic().getClass());
HttpTransportSE androidHttpTransport = new HttpTransportSE(
connection.getURL());
androidHttpTransport.call(connection.getSOAP_ACTION(), envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
System.out.println("response: " + response.toString());
} catch (Exception e) {
e.printStackTrace();
}
虽然我已尝试并成功传递对象之前使用双精度并注册它们并且能够传递byte []但是我无法传递一个带有byte []的复杂类型作为参数。
接下来会出现logcat错误:
SoapFault - faultcode: 'soapenv:Server' faultstring: 'Exception occurred while trying to invoke service method pictureWebservice' faultactor: 'null' detail: org.kxml2.kdom.Node@4153d588
12-13 22:26:51.363: W/System.err(5393): at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:112)
12-13 22:26:51.363: W/System.err(5393): at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:137)
12-13 22:26:51.363: W/System.err(5393): at org.ksoap2.transport.Transport.parseResponse(Transport.java:63)
12-13 22:26:51.363: W/System.err(5393): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:100)
12-13 22:26:51.363: W/System.err(5393): at memory.aid.webservices.WSClient.WSClientPictureFile(WSClient.java:92)
12-13 22:26:51.363: W/System.err(5393): at memory.aid.memoryaid.MemoryAid$1.run(MemoryAid.java:185)
12-13 22:26:51.363: W/System.err(5393): at android.os.Handler.handleCallback(Handler.java:615)
12-13 22:26:51.367: W/System.err(5393): at android.os.Handler.dispatchMessage(Handler.java:92)
12-13 22:26:51.367: W/System.err(5393): at android.os.Looper.loop(Looper.java:137)
12-13 22:26:51.367: W/System.err(5393): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-13 22:26:51.367: W/System.err(5393): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 22:26:51.367: W/System.err(5393): at java.lang.reflect.Method.invoke(Method.java:511)
12-13 22:26:51.375: W/System.err(5393): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-13 22:26:51.375: W/System.err(5393): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-13 22:26:51.375: W/System.err(5393): at dalvik.system.NativeStart.main(Native Method)
任何人都有任何想法,解决方案或建议吗?非常感谢你提前。
编辑:
从我可以告诉问题的是,不知何故,在服务器的webservice我调用类Pic中的imageInByte参数为null。没有其他参数为null。名称,纬度,经度,精度和时间都是有效的参数,因为imageInByte为null。为什么?请帮忙
答案 0 :(得分:0)
在你的wsdl中,你已将此字段“imageInByte”定义为简单类型,这就是为什么它会给你这个错误。
将其定义为复杂类型,包含您尝试传递的所有字段。
谢谢, ambuj