2011-08-02 228 views
1

在我的应用我想发出一个SOAP请求某些URL,将要发送的SOAP请求如下发送SOAP请求

POST /TelLink/WrenchENTService.asmx HTTP/1.1 
Host: localhost 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 
SOAPAction: "http://WrenchGlobal/GetToDoList" 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Body> 
<GetToDoList xmlns="http://WrenchGlobal/"> 
    <viPeriod>int</viPeriod> 
    <vsUserID>string</vsUserID> 
    </GetToDoList> 
</soap:Body> 
</soap:Envelope> 

在上面的代码中,我将不得不更换“INT”和带有实际值的“字符串”,它将调用服务器上的GetToDoList。我的问题是,我不知道如何将此请求发送到服务器? (使用httppost)任何人都可以帮我吗?

+1

[Android通过HTTP POST(SOAP)发送XML的可能的重复](http://stackoverflow.com/questions/2559948/android-sending-xml-via-http-post-soap) – balexandre

回答

1
int viPeriod; 
String vsUserID; 
String NAMESPACE = "http://WrenchGlobal/"; 
String SOAP_ACTION = "http://WrenchGlobal/GetToDoList"; 
String METHOD_NAME = "GetToDoList"; 

String result = null; 
Object resultRequestSOAP = null; 

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

request.addProperty("viPeriod", viPeriod); 
request.addProperty("vsUserID", vsUserID); 

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
       SoapEnvelope.VER11); 

envelope.dotNet = true; 
envelope.setOutputSoapObject(request); 

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
androidHttpTransport.debug = true; 

try { 
    androidHttpTransport.call(SOAP_ACTION, envelope); 

    String requestDumpString = androidHttpTransport.requestDump; 
    System.out.println("requestDump : " + requestDumpString); 

    resultRequestSOAP = envelope.getResponse(); // Output received 
    result = resultRequestSOAP.toString(); // Result string 

    System.out.println("OUTPUT : " + result); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

希望这会有所帮助。

+0

谢谢。上述代码中的NAMESPACE是什么?我在上面的SOAP请求中有吗?我不需要创建所有SOAP请求(在xml中)....? – Kishan

+0

我编辑了包含NAMESPACE(来自您提供的Web服务方法)和requestDump的答案。请求正以所需的xml格式发送,这可以从requestDump中理解。 – Ian

+0

在上面编辑的代码中,SoapEnvelope.VER11是什么?是否需要明确的关心,比如初始化或其他...?在行“androidHttpTransport.call(SOAP_ACTION,envelope);”我应该使用什么作为SOAP_ACTION ..? – Kishan

0

IntentServiceHttpPost可能是你想要的。 Google上有很多可用的摘要; here只是其中之一。

+0

谢谢。但我不明白在哪里实际上我必须将上述肥皂请求嵌入到httppost对象中?我实际上可以如何将肥皂请求放在httppost对象中? – Kishan

+0

在这种情况下,你最好的选择可能是图书馆。这里有一个关于SOAP和Android的非常全面的讨论:http://stackoverflow.com/questions/297586/how-to-call-soap-web-service-with-android – Paddy

+0

我想我可以用这个答案堆栈overfolw问题。 [链接](http://stackoverflow.com/questions/2559948/android-sending-xml-via-http-post-soap)。我对吗? – Kishan