using addMapping without the "i:type=" attribute in ksoap2 for android

John Neville picture John Neville · Mar 23, 2011 · Viewed 10k times · Source

I am using the envelope.addMapping function in ksoap2 and I need to make it generate items with no i:type attribute.

This is the soap request my code generates

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://www.w3.org/2003/05/soap-encoding" 
    xmlns:v="http://www.w3.org/2003/05/soap-envelope">
    <v:Header>
        <ApiKey xmlns="urn:example:data">APIKey</ApiKey>
    </v:Header>
    <v:Body>
        <CreateScan xmlns="urn:example:services" id="o0" c:root="1">
            <scan i:type="n3:" xmlns:n3="">
                <n4:BaseUrl i:type="d:string" xmlns:n5="urn:example:data">http://www.example.com</n5:BaseUrl>
                <n5:DisplayName i:type="d:string" xmlns:n7="urn:example:data">Example Scan</n7:DisplayName>
            </scan>
        </CreateScan>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I need to make it so <scan i:type="n3:scanItem" xmlns:n3=""> is generated as <scan>

Here is my code

package ksoap2.test;

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

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class ksoap2 extends Activity {
    /** Called when the activity is first created. */
    private static final String SOAP_ACTION = "http://example.com/OperationsService.svc";
    private static final String METHOD_NAME = "CreateScan";
    private static final String NAMESPACE = "urn:example:services";
    private static final String URL = "http://example.com/OperationsService.svc";
    private AndroidHttpTransport androidHttpTransport;
    TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv=(TextView)findViewById(R.id.textView1);

        try {
            Element ApiKeyElement = new Element().createElement("", "ApiKey");
            ApiKeyElement.setAttribute("", "xmlns", "urn:example:data");
            ApiKeyElement.addChild(Node.TEXT, "22DF0959F20743660304CB829B3891F0");


            Element[] header = new Element[1];
            header[0]=ApiKeyElement;


            Element request = new Element().createElement(NAMESPACE, METHOD_NAME);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);          
//              PropertyInfo scanProp = new PropertyInfo();
//              scanProp.setName("scan");
//              scanProp.setValue("");

                SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
                Scan s = new Scan();
                s.BaseUrl="http://www.example.com";
                s.DisplayName="Example";

                PropertyInfo pi = new PropertyInfo();
                pi.setName("scan");
                pi.setValue(s);
                pi.setType(s.getClass());
                Request.addProperty(pi);

                //request.addChild(Node.ELEMENT, scanElement);
                envelope.headerOut = header;
                //envelope.dotNet = true;
                envelope.setOutputSoapObject(Request);

                envelope.addMapping(null, "scanItem",new Scan().getClass());
                envelope.dotNet =false;
                String BodyClass = envelope.bodyOut.getClass().toString();
                SoapObject body = (SoapObject)envelope.bodyOut;
                int count =body.getPropertyCount();
                //envelope.encodingStyle = "test";

                //envelope.bodyOut=body;

                androidHttpTransport = new AndroidHttpTransport (URL); 
                androidHttpTransport.debug = true;
                //androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

                androidHttpTransport.call(SOAP_ACTION, envelope);
                Log.d("MyAPP", "----------------- " + androidHttpTransport.requestDump +"\r\n\r\n" + androidHttpTransport.responseDump);
                ((TextView)findViewById(R.id.textView1)).setText(androidHttpTransport.requestDump +"\r\n\r\n" + androidHttpTransport.responseDump);
            } catch(Exception E) {
                Log.d("MyAPP", "----------------- " + androidHttpTransport.requestDump +"\r\n\r\n" + androidHttpTransport.responseDump);
                ((TextView)findViewById(R.id.textView1)).setText(androidHttpTransport.requestDump+"ERROR:" +"\r\n\r\n" +androidHttpTransport.responseDump  +"\r\n\r\n" +E.getClass().getName() + ": " + E.getMessage());
            }
    }



}

From what I can tell the issue arises from the envelope.addMapping(null, "scanItem",new Scan().getClass()); line and even when I remove the "scanItem" part it still generates <scan i:type="n3:" xmlns:n3="">

Any help figuring this out would be amazing.

Answer

John Neville picture John Neville · Mar 23, 2011

Well it looks like I answered my own question. All I had to do was add the line envelope.implicitTypes = true;