在ksoap2中反序列化复杂对象数组时出错

时间:2012-11-07 01:34:24

标签: android ksoap2

我在本地tomcat服务器和客户端android上有一个web服务。我必须通过ksoap2从客户端向webservice发送一组自定义对象。 这是客户端中定义的对象代码:

    import java.io.Serializable;
import java.util.Hashtable;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;


public class Istante implements KvmSerializable, Serializable
{

private static final long serialVersionUID = 1L;

public float XCoord;
public float YCoord;
public float Pressure;
public float gap;
int action;
int idCampione;
int idAnagrafica;


public Istante() {

}

public Istante(float xCoord, float yCoord, float pressure, float Gap, int Action, int IdCampione, int IdAnagrafica) {

    this.XCoord = xCoord;
    this.YCoord = yCoord;
    this.Pressure = pressure;
    this.gap = Gap;
    this.action = Action;
    this.idCampione = IdCampione;
    this.idAnagrafica = IdAnagrafica;
}
@Override
public Object getProperty(int arg0) {

    switch (arg0) {
    case 0:
        return XCoord;
    case 1: 
        return YCoord;
    case 2:
        return Pressure;
    case 3:
        return gap;
    case 4: 
        return action;
    case 5:
        return idCampione;
    case 6:
        return idAnagrafica;
    default:
        return null;
    }
}

@Override
public int getPropertyCount() {

    return 7;
}

@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
    switch (arg0) {
    case 0:
        arg2.name = "XCoord";
        arg2.type = Float.class;
        break;
    case 1: 
        arg2.name = "YCoord";
        arg2.type = Float.class;
        break;
    case 2:
        arg2.name = "Pressure";
        arg2.type = Float.class;
        break;
    case 3:
        arg2.name = "gap";
        arg2.type = Float.class;
        break;
    case 4: 
        arg2.name = "action";
        arg2.type = PropertyInfo.INTEGER_CLASS;
        break;
    case 5:
        arg2.name = "idCampione";
        arg2.type = PropertyInfo.INTEGER_CLASS;
        break;
    case 6:
        arg2.name = "idanagrafica";
        arg2.type = PropertyInfo.INTEGER_CLASS;
        break;
    default:
        break;
    }

}

@Override
public void setProperty(int arg0, Object arg1) {
    switch (arg0) {
    case 0:
        XCoord = Float.parseFloat(arg1.toString());
        break;
    case 1: 
        YCoord = Float.parseFloat(arg1.toString());
        break;
    case 2:
        Pressure = Float.parseFloat(arg1.toString());
        break;
    case 3:
        gap = Float.parseFloat(arg1.toString());
        break;
    case 4: 
        action = Integer.parseInt(arg1.toString());
        break;
    case 5:
        idCampione = Integer.parseInt(arg1.toString());
        break;
    case 6:
        idAnagrafica = Integer.parseInt(arg1.toString());
        break;
    default:
        break;
    }

}
}


    import java.util.Hashtable;
import java.util.Vector;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class ListaIstanti extends Vector<Istante> implements KvmSerializable {

        private static final long serialVersionUID = 1L;  // you can let the IDE generate this

    @Override
    public Object getProperty(int index) {
    return this.get(index);
    }

    @Override
    public int getPropertyCount() {
    return this.size();
    }

    @Override
    public void setProperty(int index, Object value) {
    this.add((Istante) value);
    }

    @Override
    public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
    info.name = "Istante";
    info.type = Istante.class;
    }


}


public class Campione implements KvmSerializable {

private static final long serialVersionUID = 1L;

public ListaIstanti istanti;
//public int size;

public Campione() {
}


public Campione(ListaIstanti listaIstanti) {
    //size = listaIstanti.length;
    //istanti = new Istante[size];
    istanti = new ListaIstanti();
    istanti = listaIstanti;
}

@Override
public Object getProperty(int arg0) {
    switch (arg0) {
    case 0:
        return istanti;
    default:
        return null;
    }
}

@Override
public int getPropertyCount() {
    return 1;
}

@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
    switch (arg0) {
    case 0:
        arg2.name = "campione";
        arg2.type = ListaIstanti.class;
        break;
    default:
        break;
    }

}

@Override
public void setProperty(int arg0, Object arg1) {
    switch (arg0) {
    case 0:
        istanti = (ListaIstanti) arg1;
        break;
    default:
        break;
    }

}

}

这是客户代码的一部分:

    ...
METHOD_NAME = "setCampioni";
ListaIstanti lista = new ListaIstanti();
        for (int q=0; q<istanti.size(); q++){
            //camp[q] = istanti.get(q);     
            lista.add(istanti.get(q));
        } 
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
try {
 PropertyInfo pi = new PropertyInfo();
            pi.setName("Campione");
            pi.setValue(campione);
            pi.setType(Campione.class);
            request.addProperty(pi);
                    SoapEnvelope.VER11);

            envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        envelope.addMapping(NAMESPACE2, "Istante", new Istante().getClass());
        envelope.addMapping(NAMESPACE2, "Campione", new Campione().getClass());
        envelope.addMapping(NAMESPACE2, "ListaIstanti", new ListaIstanti().getClass());
        Marshal floatMarshal = new MarshalFloat();
        floatMarshal.register(envelope);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;
        } catch (Exception E) {
        E.printStackTrace();
        }
        ...

因此,这些是服务器端中相同对象的类:

import java.io.Serializable;


public class Istante implements Serializable {

private static final long serialVersionUID = 1L;

public float XCoord;
public float YCoord;
public float Pressure;
public float gap;
int action;
int idCampione;
int idAnagrafica;

public Istante() {
}

public Istante(float xCoord, float yCoord, float pressure, float Gap, int Action, int IdCampione, int IdAnagrafica) {

    this.XCoord = xCoord;
    this.YCoord = yCoord;
    this.Pressure = pressure;
    this.gap = Gap;
    this.action = Action;
    this.idCampione = IdCampione;
    this.idAnagrafica = IdAnagrafica;
}

public void setXCoord(float XCoord) {
    this.XCoord = XCoord;
}

public void setYCoord(float YCoord) {
    this.YCoord = YCoord;
}

public void setPressure(float Pressure) {
    this.Pressure = Pressure;
}

public void setGap(float gap) {
    this.gap = gap;
}

public void setAction(int action) {
    this.action = action;
}

public void setIdCampione(int idCampione) {
    this.idCampione = idCampione;
}

public void setIdAnagrafica(int idAnagrafica) {
    this.idAnagrafica = idAnagrafica;
}

public float getXCoord() {
    return XCoord;
}

public float getYCoord() {
    return YCoord;
}

public float getPressure() {
    return Pressure;
}

public float getGap() {
    return gap;
}

public int getAction() {
    return action;
}

public int getIdCampione() {
    return idCampione;
}

public int getIdAnagrafica() {
    return idAnagrafica;
}

}

import java.io.Serializable;

import java.util.Vector;


public class ListaIstanti extends Vector<Istante> implements Serializable {

private static final long serialVersionUID = 1L;  // you can let the IDE generate this




}


import java.io.Serializable;

import com.biosign.vo.ListaIstanti;


public class Campione implements Serializable {

    private static final long serialVersionUID = 1L;

    public ListaIstanti istanti = new ListaIstanti();

    public Campione() {
    }

public Istante getIstante(int index) {
return this.get(index);
}

public int getSize() {
return this.size();
}


public void setIstante(Istante value) {
this.add((Istante) value);
}
}



public void setCampione(ListaIstanti listaIstanti) {

    istanti = listaIstanti;
}

public ListaIstanti getCampione() {
    return istanti;
}

public int size() {
    return istanti.size();
}

public Istante get(int i) {
    return istanti.get(i);
}

}

这是Web服务类中的方法:

public Boolean setCampioni(Campione campioni){


          try {
            stmt = (Statement) con.createStatement();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          ListaIstanti lista = new ListaIstanti();
          lista = campioni.getCampione();
          Istante ist = new Istante();
          ist = lista.get(1);
          System.out.println("eccoci" + ist.getXCoord());
          }

因此,当我尝试发送数组的请求时,客户端序列化对象很好但在服务器端我有这个错误:

java.lang.ArrayIndexOutOfBoundsException: 1
at org.apache.axis2.databinding.utils.BeanUtil.deserialize(BeanUtil.java:630)
at org.apache.axis2.rpc.receivers.RPCUtil.processRequest(RPCUtil.java:153)
at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:206)
at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:117)
at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:114)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:181)
at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:172)
at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:146)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

并在LogCat中:

W/System.err(1306): SoapFault - faultcode: 'soapenv:Server' faultstring: '1' faultactor: 'null' detail: org.kxml2.kdom.Node@41021438

请帮助我:为什么我的网络服务无法反序列化对象?

0 个答案:

没有答案