2011-03-02 201 views
1

我必須做出一個SOAP請求,它的格式是這樣的:SOAP請求標籤

<parentname>test</parentname>  
<address>16, texas</address>  
<childrennames> 
    <child>c1</child> 
    <child>c2</child> 
    <child>c3</child>  
</childrennames>  
<email>[email protected]</email> 

如何發送的childrennames標籤的要求?

+0

你能解釋一下你想做什麼? – 2011-03-02 04:57:07

+0

請把你傳遞值的xml根據我告訴你如何將數據放入子標籤中。因爲在SOAP xml中我們使用NAMESPACE,SOAPACTION。 – 2011-03-02 05:05:31

回答

2

最簡單的方法之一是將您的整個SOAP請求放在StringBuilder中,並使用一些唯一鍵代替實際值。用運行時的實際值替換您的密鑰,使用StringBuilder#replaceAll(..),然後通過將該實體的內容類型設置爲XML,將該字符串作爲StringEntity發佈到服務器。 對於e.g:您可以創建一個模板:

"<parentname>@[email protected]</parentname>" 

,然後通過實際值在運行時更換@[email protected]

您可以使用SOAPUI等工具來生成整個SOAP請求XML。 希望你得到一張照片。

[編輯:添加例如用於說明] 假如你要創建這個SOAP請求:

<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:bok="http://www.example.org/bok/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <bok:Name> 
     <name>Foucault's Pendulum</name> 
     </bok:Name> 
    </soapenv:Body> 
</soapenv:Envelope> 

我建議是建立一個String模板,然後在運行時的模板替換值:

// your SOAP request template 
StringBuilder SOAP_REQUEST = 
new StringBuilder("<soapenv:Envelope 
xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' 
xmlns:bok='http://www.example.org/bok/'> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <bok:Name> 
     <name>@[email protected]</name> 
     </bok:Name> 
    </soapenv:Body> 
</soapenv:Envelope>"); 

// replacing keys with actual arguments at runtime 
SOAP_REQUEST.toString().replace("@name",actualName); 

我希望這是有道理的。

+0

它不工作..任何其他選項? – 2011-03-03 08:45:08

+0

不工作是什麼意思?我在一些項目中使用了這種技術。你能用你試過的東西來更新你的問題嗎? – Samuh 2011-03-03 09:09:49

+0

C1 C2 C3 在這裏,我要在我的原代碼,通過整數值..有驗證,所以我無法通過這些標籤作爲附加串 – 2011-03-04 05:09:29

0

對於簡單的SOAP請求:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    request.addProperty("Celsius", "32"); 

    SoapSerializationEnvelope soapEnvelop = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    soapEnvelop.dotNet = true; 
    soapEnvelop.setOutputSoapObject(request); 
    AndroidHttpTransport aht = new AndroidHttpTransport(URL); 

    try { 

     aht.call(SOAP_ACTION , soapEnvelop); 

     SoapObject resultString = (SoapObject) soapEnvelop.getResponse(); 
     } catch (Exception e) { 
     e.printStackTrace(); 
    } 

導入另一個定製AndroidHttpTransport文件:

import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.transport.ServiceConnection; 
import org.ksoap2.transport.Transport; 
import org.xmlpull.v1.XmlPullParserException; 
/** 
* Apache HttpComponent based HttpTransport layer. 
*/ 
public class AndroidHttpTransport extends Transport { 

    /** 
    * Creates instance of HttpTransport with set url 
    * 
    * @param url 
    *   the destination to POST SOAP data 
    */ 
    public AndroidHttpTransport(String url) { 
     super(url); 
    } 

    /** 
    * set the desired soapAction header field 
    * 
    * @param soapAction 
    *   the desired soapAction 
    * @param envelope 
    *   the envelope containing the information for the soap call. 
    */ 
    public void call(String soapAction, SoapEnvelope envelope) throws IOException, XmlPullParserException { 
     if (soapAction == null) 
      soapAction = "\"\""; 
     byte[] requestData = createRequestData(envelope); 
     requestDump = debug ? new String(requestData) : null; 
     responseDump = null; 
     ServiceConnection connection = getServiceConnection(); 
     connection.connect(); 

     try { 
      connection.setRequestProperty("User-Agent", "kSOAP/2.0"); 
      connection.setRequestProperty("SOAPAction", soapAction); 
      connection.setRequestProperty("Content-Type", "text/xml"); 
      connection.setRequestProperty("Connection", "close"); 
      connection.setRequestProperty("Content-Length", "" + requestData.length); 
      connection.setRequestMethod("POST"); 

      OutputStream os = connection.openOutputStream(); 
      os.write(requestData, 0, requestData.length); 
      os.flush(); 
      os.close(); 
      requestData = null; 

      InputStream is; 
      try { 
       is = connection.openInputStream(); 
      } catch (IOException e) { 
       is = connection.getErrorStream(); 
       if (is == null) { 
        connection.disconnect(); 
        throw (e); 
       } 
      } 


      //if (debug) { 
      if (true) { 
       ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
       byte[] buf = new byte[512]; 
       while (true) { 
        int rd = is.read(buf, 0, 512); 
        if (rd == -1) 
         break; 
        bos.write(buf, 0, rd); 
       } 
       bos.flush(); 
       buf = bos.toByteArray(); 
       responseDump = new String(buf); 
       CommonFunctions.currentXMLString = unescape(responseDump); 
       //CommonFunctions.currentXMLString = responseDump; 
       is.close(); 
       is = new ByteArrayInputStream(buf); 
      } 

      parseResponse(envelope, is); 
     } finally { 
      connection.disconnect(); 
     } 
    } 

    protected ServiceConnection getServiceConnection() throws IOException { 
     return new AndroidServiceConnection(url); 
    } 

    public static String unescape (String s) 
    { 
     while (true) 
     { 
      int n=s.indexOf("&#"); 
      if (n<0) break; 
      int m=s.indexOf(";",n+2); 
      if (m<0) break; 
      try 
      { 
       s=s.substring(0,n)+(char)(Integer.parseInt(s.substring(n+2,m)))+ 
        s.substring(m+1); 
      } 
      catch (Exception e) 
      { 
       return s; 
      } 
     } 
     s=s.replace("&quot;","\""); 
     s=s.replace("&lt;","<"); 
     s=s.replace("&gt;",">"); 
     s=s.replace("&amp;","&"); 
     return s; 
    } 
} 

請添加一個自定義AndroidServiceConnection.java如下

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

import org.apache.commons.httpclient.HostConfiguration; 
import org.apache.commons.httpclient.HttpConnection; 
import org.apache.commons.httpclient.HttpConnectionManager; 
import org.apache.commons.httpclient.HttpState; 
import org.apache.commons.httpclient.HttpURL; 
import org.apache.commons.httpclient.SimpleHttpConnectionManager; 
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; 
import org.apache.commons.httpclient.methods.PostMethod; 
import org.apache.commons.httpclient.methods.RequestEntity; 
import org.ksoap2.transport.ServiceConnection; 


/** 
* Connection using apache HttpComponent 
*/ 

public class AndroidServiceConnection implements ServiceConnection { 
    private static HttpConnectionManager connectionManager = new SimpleHttpConnectionManager(); 
    private HttpConnection connection; 
    private PostMethod postMethod; 
    private java.io.ByteArrayOutputStream bufferStream = null; 

    /** 
    * Constructor taking the url to the endpoint for this soap communication 
    * @param url the url to open the connection to. 
    */ 
    public AndroidServiceConnection(String url) throws IOException { 
     HttpURL httpURL = new HttpURL(url); 
     HostConfiguration host = new HostConfiguration(); 
     host.setHost(httpURL.getHost(), httpURL.getPort()); 
     connection = connectionManager.getConnection(host); 
     postMethod = new PostMethod(url); 
    } 

    public void connect() throws IOException { 
     if (!connection.isOpen()) { 
      connection.open(); 
     } 
    } 

    public void disconnect() { 
     connection.releaseConnection(); 
    } 

    public void setRequestProperty(String name, String value) { 
     postMethod.setRequestHeader(name, value); 
    } 

    public void setRequestMethod(String requestMethod) throws IOException { 
     if (!requestMethod.toLowerCase().equals("post")) { 
      throw(new IOException("Only POST method is supported")); 
     } 
    } 

    public OutputStream openOutputStream() throws IOException { 
     bufferStream = new java.io.ByteArrayOutputStream(); 
     return bufferStream; 
    } 

    public InputStream openInputStream() throws IOException { 
     RequestEntity re = new ByteArrayRequestEntity(bufferStream.toByteArray()); 
     postMethod.setRequestEntity(re); 
     postMethod.execute(new HttpState(), connection); 
     return postMethod.getResponseBodyAsStream(); 
    } 

    public InputStream getErrorStream() { 
     return null; 
    } 
}