iehsan77 / ksoap2-android

Automatically exported from code.google.com/p/ksoap2-android
0 stars 0 forks source link

POJO support in ksoap2? #86

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm trying to connect to xfire1.2.6 webservice.

The webservice definition is:
-------------------------
public interface Echo
{
    String echo(String msg);
    Pojo echoPojo(Pojo pojo);
}
-------------------------

To echo method, ksoap2 supports it well but the second one, echoPojo doesn't.

My code is:
------------------------
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

Pojo p = new Pojo();
p.setA("AAA");
p.setB("BBB");      
PropertyInfo propertyInfo = new PropertyInfo();
propertyInfo.setName("n0");
propertyInfo.setValue(p);
propertyInfo.setType(p.getClass());
propertyInfo.setNamespace(NAMESPACE);
request.addProperty(propertyInfo);

SoapSerializationEnvelope soapEnvelope = new 
SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.setOutputSoapObject(request);

soapEnvelope.addMapping(NAMESPACE, "Pojo", new Pojo().getClass());

HttpTransportSE transport =new HttpTransportSE (URL);

try{
    transport.debug = true;
    Log.i("Examples", "Connecting to internet...");
    transport.call(SOAP_ACTION, soapEnvelope);
    Pojo response = (Pojo)soapEnvelope.bodyIn;
    Log.i("Examples", "Result:"+response);     
} catch(Exception e){
    Log.e("Examples", e.getMessage());
    e.printStackTrace();
}
------------------------

The Pojo.java has implemented the KvmSerializable interface:
------------------------

package bright.zheng.examples.ksoap2;

import java.util.Hashtable;

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

public class Pojo implements KvmSerializable{
    String a;
    String b;

    public String getA() {
        return a;
    }
    public void setA(String a) {
        this.a = a;
    }
    public String getB() {
        return b;
    }
    public void setB(String b) {
        this.b = b;
    }

    public Object getProperty(int arg0) {

        switch(arg0)
        {
        case 0:
            return a;
        case 1:
            return b;
        }

        return null;
    }

    public int getPropertyCount() {
        return 2;
    }

    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
        switch(index)
        {
        case 0:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "a";
            break;
        case 1:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "b";
            break;
        default:break;
        }
    }

    public void setProperty(int index, Object value) {
        switch(index)
        {
        case 0:
            a = value.toString();
            break;
        case 1:
            b = value.toString();
            break;
        default:
            break;
        }
    }
}
------------------------

I'm using TCPMon to get the soap message and found that the soap message is as:
------------------------
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:d="http://www.w3.org/2001/XMLSchema" 
xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
  <v:Header />
  <v:Body>
    <n0:echoPojo id="o0" c:root="1" xmlns:n0="http://example.spring.xfire.codehaus.org">
      <n0:n0 i:type="n0:Pojo">
        <a i:type="d:string">AAA</a>
        <b i:type="d:string">BBB</b>
      </n0:n0>
    </n0:echoPojo>
  </v:Body>
</v:Envelope>
------------------------

If I change the soap message as following, it works.
------------------------
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:d="http://www.w3.org/2001/XMLSchema" 
xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
  <v:Header />
  <v:Body>
    <n0:echoPojo id="o0" c:root="1" xmlns:n0="http://example.spring.xfire.codehaus.org">
      <n0:n0 i:type="n0:Pojo">
        <a i:type="d:string" xmlns="http://example.spring.xfire.codehaus.org">AAA</a>
        <b i:type="d:string" xmlns="http://example.spring.xfire.codehaus.org">BBB</b>
      </n0:n0>
    </n0:echoPojo>
  </v:Body>
</v:Envelope>
------------------------

What difference is that I added the namespace to the Pojo's properties(e.g a/b 
here).

Maybe I missed something to get POJO support by using ksoap2?

Please advice. Thanks.

Original issue reported on code.google.com by bright.z...@gmail.com on 13 Oct 2011 at 2:40

GoogleCodeExporter commented 9 years ago
Try setting addAdornments on SoapSerializationEnvelope to false

Original comment by mosa...@gmail.com on 18 Nov 2011 at 5:41

GoogleCodeExporter commented 9 years ago
If I want to send and receive a POJO I always use:

Pojo pojo = new Pojo();
pojo.setAttr1(attr1);

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty(pojo.getClass().getSimpleName(), pojo);
SoapSerializationEnvelope envelope = new 
SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping(THE_POJO_NAMESPACE, pojo.getClass().getSimpleName(), 
pojo.getClass());

HttpTransportSE transport = new HttpTransportSE(URL);
transport.call(NAMESPACE + METHOD_NAME, envelope);
Pojo pojo_response = (Pojo) envelope.getResponse();

THE_POJO_NAMESPACE is something like this (I'm using Axis2 for web services, it 
maybe different for you):
http://entities.domain.myapp.org/xsd

--------
@enrmarc

Original comment by enrm...@gmail.com on 9 Dec 2011 at 11:07

GoogleCodeExporter commented 9 years ago
So you are saying it works just fine basically?

Original comment by mosa...@gmail.com on 9 Dec 2011 at 11:19

GoogleCodeExporter commented 9 years ago
Closing due to no feedback

Original comment by mosa...@gmail.com on 30 Apr 2012 at 6:29