How to get Paramentro in a Web service in C # sent from Android

1

I'm consuming a Web service developed in C #, from an Android application. When sending simple types per parameter such as string, int, or long, I can get the information usually just by putting the same type in the Web service method declaration parameter. But I do not know how to get a complex object created in java within the web service method in C #. The submitted object has the same name in both applications, but they are not exactly the same because of property names.

-I'm using Ksoap2 on android, and the class is being serialized and sent to the web service without any errors. -maybe I could get the object as XML, but what kind would I put on the face?

Call web service

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        PropertyInfo object = new PropertyInfo();
        object.setName("obj");
        object.setValue(ordemServicoBO);
        object.setType(ordemServicoBO.getClass());

        request.addProperty(object);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.encodingStyle = SoapSerializationEnvelope.ENC;
        envelope.setOutputSoapObject(request);
        envelope.addMapping(NAMESPACE, "ordemServicoBO",new OrdemServicoBO().getClass());
        envelope.implicitTypes = true;
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;
        androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

        androidHttpTransport.call(SOAP_ACTION, envelope);//Aqui a chamada é feita normalmente sem erro, e entra no método do web service

        SoapObject responseNoXML = (SoapObject)envelope.bodyIn;

Java class

public class OrdemServicoBO implements KvmSerializable {

private long idOrdemServico;
private long IdOrdemServicoCategoria;
private long idOrdemServicoLocal;
private long idHotel;
private long idUsuarioExecutante;
private long idUsuarioSolicitante;
private long idArea;
private String dataAbertura;
private String descricao;
private String status;
private AreaBO areaBO;
private UsuarioBO usuarioSolicitante;
private UsuarioBO usuarioExecutante;
private OrdemServicoCategoriaBO ordemServicoCategoriaBO;

public void setOrdemServicoCategoriaBO(OrdemServicoCategoriaBO ordemServicoCategoriaBO) {
    this.ordemServicoCategoriaBO = ordemServicoCategoriaBO;
}

public OrdemServicoCategoriaBO getOrdemServicoCategoriaBO() {
    return ordemServicoCategoriaBO;
}

public void setUsuarioExecutante(UsuarioBO usuarioExecutante) {
    this.usuarioExecutante = usuarioExecutante;
}

public void setUsuarioSolicitante(UsuarioBO usuarioSolicitante) {
    this.usuarioSolicitante = usuarioSolicitante;
}

public UsuarioBO getUsuarioExecutante() {
    return usuarioExecutante;
}

public UsuarioBO getUsuarioSolicitante() {
    return usuarioSolicitante;
}

public void setDataAbertura(String dataAbertura) {
    this.dataAbertura = dataAbertura;
}

public void setDescricao(String descricao) {
    this.descricao = descricao;
}

public void setIdArea(long idArea) {
    this.idArea = idArea;
}

public void setIdHotel(long idHotel) {
    this.idHotel = idHotel;
}

public void setIdOrdemServico(long idOrdemServico) {
    this.idOrdemServico = idOrdemServico;
}

public void setIdOrdemServicoCategoria(long idOrdemServicoCategoria) {
    IdOrdemServicoCategoria = idOrdemServicoCategoria;
}

public void setIdOrdemServicoLocal(long idOrdemServicoLocal) {
    this.idOrdemServicoLocal = idOrdemServicoLocal;
}

public void setIdUsuarioExecutante(long idUsuarioExecutante) {
    this.idUsuarioExecutante = idUsuarioExecutante;
}

public void setIdUsuarioSolicitante(long idUsuarioSolicitante) {
    this.idUsuarioSolicitante = idUsuarioSolicitante;
}

public void setStatus(String status) {
    this.status = status;
}

public String getDataAbertura() {
    return dataAbertura;
}

public long getIdArea() {
    return idArea;
}

public long getIdHotel() {
    return idHotel;
}

public long getIdOrdemServico() {
    return idOrdemServico;
}

public long getIdOrdemServicoCategoria() {
    return IdOrdemServicoCategoria;
}

public long getIdOrdemServicoLocal() {
    return idOrdemServicoLocal;
}

public long getIdUsuarioExecutante() {
    return idUsuarioExecutante;
}

public long getIdUsuarioSolicitante() {
    return idUsuarioSolicitante;
}

public String getDescricao() {
    return descricao;
}

public String getStatus() {
    return status;
}

public void setAreaBO(AreaBO areaBO) {
    this.areaBO = areaBO;
}

public AreaBO getAreaBO() {
    return areaBO;
}

@Override
public Object getProperty(int arg0)
{
    switch(arg0)
    {
        case 0:
            return getDataAbertura();
        case 1:
            return getDescricao();
        case 2:
            return getStatus();
        case 3:
            return getIdOrdemServicoCategoria();
        case 4:
            return getIdOrdemServico();
        case 5:
            return getIdOrdemServicoLocal();
        case 6:
            return getIdArea();
        case 7:
            return getIdHotel();
        case 8:
            return getIdUsuarioExecutante();
        case 9:
            return getIdUsuarioSolicitante();

    }
    return null;
}
@Override
public void setProperty(int arg0, Object arg1)
{
    switch(arg0)
    {
        case 0:
            setDataAbertura(arg1.toString());
            break;
        case 1:
            setDescricao(arg1.toString());
            break;
        case 2:
            setStatus(arg1.toString());
            break;
        case 3:
            setIdOrdemServicoCategoria(Long.parseLong(arg1.toString()));
            break;
        case 4:
            setIdOrdemServico(Long.parseLong(arg1.toString()));
            break;
        case 5:
            setIdOrdemServicoLocal(Long.parseLong(arg1.toString()));
            break;
        case 6:
            setIdArea(Long.parseLong(arg1.toString()));
            break;
        case 7:
            setIdHotel(Long.parseLong(arg1.toString()));
            break;
        case 8:
            setIdUsuarioExecutante(Long.parseLong(arg1.toString()));
            break;
        case 9:
            setIdUsuarioSolicitante(Long.parseLong(arg1.toString()));
            break;
    }

}
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2)
{
    switch(arg0)
    {
        case 0:
            arg2.type = PropertyInfo.STRING_CLASS;
            arg2.name = "CategoryId";
            break;
        case 1:
            arg2.type = PropertyInfo.STRING_CLASS;
            arg2.name = "Description";
            break;
        case 2:
            arg2.type = PropertyInfo.STRING_CLASS;
            arg2.name = "Name";
            break;
        case 3:
            arg2.type = PropertyInfo.INTEGER_CLASS;
            arg2.name = "CategoryId";
            break;
        case 4:
            arg2.type = PropertyInfo.INTEGER_CLASS;
            arg2.name = "CategoryId";
            break;
        case 5:
            arg2.type = PropertyInfo.INTEGER_CLASS;
            arg2.name = "CategoryId";
            break;
        case 6:
            arg2.type = PropertyInfo.INTEGER_CLASS;
            arg2.name = "CategoryId";
            break;
        case 7:
            arg2.type = PropertyInfo.INTEGER_CLASS;
            arg2.name = "CategoryId";
            break;
        case 8:
            arg2.type = PropertyInfo.INTEGER_CLASS;
            arg2.name = "CategoryId";
            break;
        case 9:
            arg2.type = PropertyInfo.INTEGER_CLASS;
            arg2.name = "CategoryId";
            break;

        default:break;
    }

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

Web service method C #

[WebMethod]
public OrdemServicoBO Exportar(OrdemServicoBO ordemServicoBO)//quando é executado o androidHttpTransport.call() é chamado esse método, mas o retorno, que é o mesno recebido por parâmetro, esta vazio.
{
    return ordemServicoBO; //Atualmente retorna vazio
}

Class in C #

public class OrdemServicoBOAPP
{
    private long _idOrdemServico;

    public long IdOrdemServico
    {
        get { return _idOrdemServico; }
        set { _idOrdemServico = value; }
    }
    private long _IdOrdemServicoCategoria;

    public long IdOrdemServicoCategoria
    {
        get { return _IdOrdemServicoCategoria; }
        set { _IdOrdemServicoCategoria = value; }
    }
    private long _idOrdemServicoLocal;

    public long IdOrdemServicoLocal
    {
        get { return _idOrdemServicoLocal; }
        set { _idOrdemServicoLocal = value; }
    }
    private long _idHotel;

    public long IdHotel
    {
        get { return _idHotel; }
        set { _idHotel = value; }
    }
    private long _idUsuarioExecutante;

    public long IdUsuarioExecutante
    {
        get { return _idUsuarioExecutante; }
        set { _idUsuarioExecutante = value; }
    }
    private long _idUsuarioSolicitante;

    public long IdUsuarioSolicitante
    {
        get { return _idUsuarioSolicitante; }
        set { _idUsuarioSolicitante = value; }
    }
    private long _idArea;

    public long IdArea
    {
        get { return _idArea; }
        set { _idArea = value; }
    }
    private String _dataAbertura;

    public String DataAbertura
    {
        get { return _dataAbertura; }
        set { _dataAbertura = value; }
    }
    private string _descricao;

    public string Descricao
    {
        get { return _descricao; }
        set { _descricao = value; }
    }
    private string _status;

    public string Status
    {
        get { return _status; }
        set { _status = value; }
    }
    private AreaBOAPP _areaBO;

    public AreaBOAPP AreaBO
    {
        get { return _areaBO; }
        set { _areaBO = value; }
    }
}
    
asked by anonymous 28.03.2014 / 15:55

1 answer

2

By your code it seems that you have already learned how to implement the KvmSerializable interface to map complex objects to XML and vice versa. Otherwise, see this example of Wsdl2Code (which, by the way, is a great tool for generating this type of < in> boilerplate code for you). Once you have the mapping of the complex objects ready, the call is fairly simple:

You've already added a mapping of a a name to a class. In this case I do not know if ordemServicoBO is a name of an element in the call or in the response of your service. For the Exportar service you need to map the two to the same object type ( OrdemServicoBO ):

// Estou assumindo algumas coisas sobre o WSDL e o serviço Exportar
// O nome "ordemServicoBO" podem ser completamente diferente
// Em alguns casos pode ser que o request ou a resposta estejam encapsuladas, em
// outro objeto, etc
envelope.addMapping(NAMESPACE, "ordemServicoBO", new OrdemServicoBO().getClass());

// Veja que novamente o nome desa propriedade depende do seu WSDL
request.addProperty("ordemServicoBO", ordemServicoBO);

When making the call, assuming that the mappings are correct, your .NET-side service will receive a complete object:

 androidHttpTransport.call(SOAP_ACTION, envelope);

Once again assuming that your WSDL returns a complex type of response directly in the body of the response, to recover the value on the Android side just do a cast of that object:

 OrdemServicoBO result = (OrdemServicoBO) envelope.getResponse();

If there is any kind of indirection you will need to handle this manually (again see Wsdl2Code example )

Getting XML generated for debugging effect:

androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);

androidHttpTransport.requestDump; // string contendo a chamada
androidHttpTransport.responseDump // string contendo a resposta

TL; DR

  • You are already mapping complex types through the interface ordemServicoBO
  • It's now a matter of getting mappings between namespaces , names and classes according to your WSDL.
  • Use the KvmSerializable and requestDump properties to get Strings containing XML

Tips:

  • Use an external client such as SoapUI to test calls and responses. Set the responseDump until the OrdemServicoBO variable is identical to the SoapUI request XML.

  • Check your WSDL ( example ), all elements and mappings should respond to requestDump in the names tags.

  • The Wsdl2Code tool can generate all of this for you.

Source: ksoap2-android - Coding Tips And Tricks

    
30.03.2014 / 01:10