2014-12-30 59 views
1

嗨,我有一個代碼生成一個簡單的請求,我需要建立一個像一個請求,例如SOAP服務器:ksoap2 v prefixo任何其他前綴

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://10.1.5.80:8080/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <ns:GETSERVERTIME/> 
    </soapenv:Body> 
</soapenv:Envelope> 

,但我得到

<v:Envelope 
    xmlns:i="http://www.w3.org/1999/XMLSchema-instance" 
    xmlns:d="http://www.w3.org/1999/XMLSchema" 
    xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"> 
<v:Header /> 
    <v:Body> 
     <n0:GETSERVERTIME xmlns:n0="http://localhost:8080/" /> 
    </v:Body> 
</v:Envelope> 

我只需要改變 「v」 到 「soapenv:」

我的代碼:

/** 
* Created by Vinicius Gati on 30/12/14. 
* 
*/ 
public class ServerSOAP { 

    private static final String METHOD_NAME = "GETSERVERTIME"; 
    private static final String NAMESPACE = "http://localhost:8080/"; 
    private static final String SOAP_ACTION = ""; 
    private static final String URL = "http://10.1.5.80:8080/ws/SERVERTIME.apw?WSDL"; 

    public static String getServerTime() { 
     String retorno = ""; 

     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); 
     envelope.implicitTypes = false; 
     envelope.setAddAdornments(false); 

     envelope.setOutputSoapObject(request); 
     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
     androidHttpTransport.debug = true; 

     try { 
      androidHttpTransport.call(NAMESPACE + METHOD_NAME, envelope); 
      SoapObject response = (SoapObject) envelope.getResponse(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return retorno; 
    } 

} 

但我瘋了試試這個,沒成功。

回答

1

正如您可能會看到: https://github.com/mosabua/ksoap2-android/blob/master/ksoap2-base/src/main/java/org/ksoap2/SoapEnvelope.java

寫作方法前綴定義爲字符串常量(從類的SoapEnvelope代碼副本,請參閱提供的鏈接):

public void write(XmlSerializer writer) throws IOException { 
    writer.setPrefix("i", xsi); 
    writer.setPrefix("d", xsd); 
    writer.setPrefix("c", enc); 
    writer.setPrefix("v", env); 
    writer.startTag(env, "Envelope"); 
    writer.startTag(env, "Header"); 
    writeHeader(writer); 
    writer.endTag(env, "Header"); 
    writer.startTag(env, "Body"); 
    writeBody(writer); 
    writer.endTag(env, "Body"); 
    writer.endTag(env, "Envelope"); 
} 

所以,你可以嘗試定義自己類,從SoapSerializationEnvelope繼承並嘗試重新定義此方法以使用「soapenv」前綴。

順便說一句:如果WS無法讀取任何名稱的前綴,它在這個服務端的糟糕的代碼。 「soapenv」或「v」應該被解釋爲在您包括的兩個xml中都是相同的。

馬辛

+0

是的,我知道,這應該工作,但遺憾的是,從我們使用不使用v前綴工作ERP SOAP協議..我花了好幾個小時才發現...謝謝我會嘗試解決方案。 –

0

哥們,

我已經解決了這個問題。其實我已經擴展SoapSerializationEnvelope類,並覆蓋了SoapSerializationEnvelopewrite()方法如下

@Override 
public void write(XmlSerializer writer) throws IOException { 
    Log.d("DEBUG", "XML create process started"); 
    env = "http://schemas.xmlsoap.org/soap/envelope/"; 
    String tem = "http://tempuri.org/"; 
    String ner = "http://schemas.datacontract.org/2004/07/MYSERVICE"; 
    //writer.text("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 
    // Don't try like above, it will cause errors, for instead of this, you should use below code 
    writer.startDocument("UTF-8", true); 
    writer.setPrefix("soapenv", env); //these are propably same for you 
    writer.setPrefix("tem", tem); //these are propably same for you 
    writer.setPrefix("ner", ner); // but this must change with your namespace writer.setPrefix("myTag", ner); 
    writer.startTag(env, "Envelope"); 
    writer.startTag(env, "Header"); 
    writeHeader(writer); 
    writer.endTag(env, "Header"); 
    writer.startTag(env, "Body"); 
    writer.startTag(tem, "Method"); 
    writer.startTag(tem, "inputObject"); 
    writer.startTag(ner, "property"); 
    writer.text("property value"); 
    writer.endTag(ner, "property"); 
    writeBody(writer); 
    writer.endTag(tem, "inputObject"); 
    writer.endTag(tem, "Method"); 
    writer.endTag(env, "Body"); 
    writer.endTag(env, "Envelope"); 
    writer.endDocument(); 
    Log.d("DEBUG","XML created"); 
} 
相關問題