使用addMapping而不是ksoap2中的“i:type =”属性for android

时间:2011-03-23 18:39:52

标签: android web-services soap ksoap2 android-ksoap2

我在ksoap2中使用envelope.addMapping函数,我需要让它生成没有i:type属性的项目。

这是我的代码生成的soap请求

<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>

我需要这样做,<scan i:type="n3:scanItem" xmlns:n3="">生成为<scan>

这是我的代码

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());
            }
    }



}

据我所知,问题来自于envelope.addMapping(null, "scanItem",new Scan().getClass());行,即使删除“scanItem”部分,它仍会生成<scan i:type="n3:" xmlns:n3="">

任何帮助解决这个问题都会很棒。

1 个答案:

答案 0 :(得分:18)

好像我回答了我自己的问题。我所要做的只是添加行envelope.implicitTypes = true;