关于Android的KSoap2的使用

蒋向笛
2023-12-01

直接对SoapObject传入NameSpace 和 MethodName之后,生成的报文类似于:

...

...(省略其他)

<n0:MethodName xmlns:n0="....">

....(参数省略)

.....

</n0:MethodName>


有些webservice对这种报文能够正确的识别和解释。但是有的不能(能够访问到相应的方法,但是不能设置相应的参数的值)。

经过对KSoap2的源代码的分析,最后找到了适合项目需求的解决方案:

修改源代码文件的org/ksoap2/serialization/SoapSerializationEnvelop.java中的writeBody()方法,如下:

public void writeBody(XmlSerializer writer) throws IOException
	{
		// allow an empty body without any tags in it
		// see http://code.google.com/p/ksoap2-android/issues/detail?id=77
		if (bodyOut != null) {
            multiRef = new Vector();
            multiRef.addElement(bodyOut);
            Object[] qName = getInfo(null, bodyOut);
            
            /****
             * Modify  : ZhaoTianbao
             * Date	   : 2011-11-03
             * Comments: For using it into my app,I found it not properly worked.
             * 			So,I change this.
             * 			Delete the commemt-line,and Add some lines. 
             */
            /* Delete line */
//            writer.startTag((dotNet) ? "" : (String) qName[QNAME_NAMESPACE], (String) qName[QNAME_TYPE]);
            
            if (dotNet)/** Maybe this if condition is not the necessary @@**/
            {	
            	/* Add 1ine */
            	writer.startTag((dotNet) ? "" : (String) qName[QNAME_NAMESPACE], (String) qName[QNAME_TYPE]);//add
                writer.attribute(null, "xmlns", (String) qName[QNAME_NAMESPACE]);
            }
            
            /** The following lines add the <MethodName xmlns="..."> **/
            if (addAdornments)
            {	
            	/* Add line */
            	writer.startTag(null, (String) qName[QNAME_TYPE]);//add:add "MethodName" attribute 
            	writer.attribute(null, "xmlns", 
            			"http:/....");//add:add "xmlns=..."
            	
            	/*The following two lines may not matter @@*/
                writer.attribute(null, ID_LABEL, qName[2] == null ? ("o" + 0) : (String) qName[2]);
                writer.attribute(enc, ROOT_LABEL, "1");
                
                /* Add line */
                writeElement(writer, bodyOut, null, qName[QNAME_MARSHAL]);//add
                writer.endTag(null, (String) qName[QNAME_TYPE]);//add
            }else{
            	/* Add 1ine */
            	writer.startTag((dotNet) ? "" : (String) qName[QNAME_NAMESPACE], (String) qName[QNAME_TYPE]);//add
            
            	writeElement(writer, bodyOut, null, qName[QNAME_MARSHAL]);
            	writer.endTag((dotNet) ? "" : (String) qName[QNAME_NAMESPACE], (String) qName[QNAME_TYPE]);
            }
        }


 类似资料: