How to send an Arraylist<Object> to Webservice SOAP

Asked

Viewed 149 times

0

Good Afternoon

Assuming I have the following Java Order and Item classes:

public class Pedido implements KvmSerializable, Serializable {

private String numero;
private ArrayList<Item> itens;

public String getNumero(){
    return numero;
}

public void setNumero(String numero){
    this.numero = numero;
}

public ArrayList<Item> getItens(){
    return itens;
}

public void setItens(ArrayList<Item> itens){
    this.itens = itens;
}

public Pedido(){

}
}

public class Item implements KvmSerializable, Serializable {

private String codigo;
private double valor;

public String getCodigo(){
    return codigo;
}

public void setCodigo(String codigo){
    this.codigo = codigo;
}


public double getValor(){
    return valor;
}

public void setValor(double valor){
    this.valor = valor;
}

public Item(){

}
}

What is the procedure for sending a Request object with multiple items to a Webservice using SOAP technology, using the KSOAP2 library via Android?

I can make the request normally with objects that do not involve Arrays, but when this happens I get the following error message:

W/System.err: java.lang.Runtimeexception: Cannot serialize:

From now on, thank you.

2 answers

0

change public class Pedido { for public class Pedido implements Serializable {

and

public class Item { for public class Item implements Serializable {

If your classes are not serialized they cannot be sent to the backend, this should already solve the problem :D

  • I’m already implementing the Serializable interface in the classes, I forgot to put in the code here, I’ll update. But I still have the same problem, I can’t serialize an object array.

  • The error remains the same ? or changed ?

  • It remains the same, I can serialize both the Request object and the Item object, provided that separately. My problem is serializing a Request object with one or more Items inside it.

0


Good Afternoon

I managed to solve my problem. I’m not sure why the KSOAP2 library acts this way, but there’s no way I could serialize an array automatically as I do with primitive type attributes.

The solution I found was to pass the list as a separate parameter and add to the object when mounting the request. Following the example of the above class, the solution would be:

private final String NAMESPACE = "SEU_NAMESPACE";    

public void SeuMetodo(Pedido pedido, ArrayList<Item> itens) {
    SoapObject request = new SoapObject(NAMESPACE, "SeuMetodo");
    SoapObject ped = new SoapObject(NAMESPACE, "pedido");
    SoapObject arrayItens = new SoapObject(NAMESPACE, "Itens");

    for (Item item : itens){
        arrayItens.addProperty("Item", item);
    }

    ped.addProperty("numero", pedido.getNumero());
    ped.addProperty("Itens", arrayItens);

    request.addProperty("pedido", ped);

    try {
        //PROCEDIMENTOS DE ENVIO DA REQUISIÇÃO E RECEBIMENTO DA RESPOSTA
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I hope I’ve been able to be clear, if anyone else has that problem and needs help I’m available.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.