1

In my application I want to send a SOAP request to some url, the SOAP request to be sent is as below

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>

In the above code, I will have to replace "int" and "string" with actual values and it will invoke GetToDoList on the server. My problem is, I dont know how to send this request to the server? (using httppost) Could anyone help me out?

Sathyajith Bhat
  • 19,739
  • 21
  • 90
  • 126
Kishan
  • 449
  • 5
  • 20
  • 1
    possible duplicate of [Android, sending XML via HTTP POST (SOAP)](http://stackoverflow.com/questions/2559948/android-sending-xml-via-http-post-soap) – balexandre Aug 02 '11 at 09:19

2 Answers2

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

Hope this would help.

Kannan Suresh
  • 4,517
  • 3
  • 32
  • 58
  • thank you. What is that NAMESPACE in the above code..? Do i have it in the SOAP request above...? Dont i need to create all that SOAP request(in xml)....? – Kishan Aug 02 '11 at 09:40
  • I have edited the answer to include the NAMESPACE (from the web service method you have provided) and the requestDump. The request is being send in the required xml format, which could be understood from the requestDump. – Kannan Suresh Aug 02 '11 at 10:06
  • In the above edited code, what is SoapEnvelope.VER11 ? Is it something that needs explicite care, like initialization or something...? In line, "androidHttpTransport.call(SOAP_ACTION, envelope);" what should i use as SOAP_ACTION..? – Kishan Aug 02 '11 at 10:26
  • The version parameter must be set to one of VER10, VER11 or VER12. You may refer the following link to know more about the SoapEnvelope class: http://ksoap2.sourceforge.net/doc/api/org/ksoap2/SoapEnvelope.html – Kannan Suresh Aug 02 '11 at 10:59
  • What all to import in my application in order to use ksoap2 ? I have added ksoap2 library to my application already. When i used your code, its giving messages saying "can not be resolved". – Kishan Aug 05 '11 at 17:41
  • "Can not be resolved". Do you mean the code doesn't compile? If so try by adding the ksoap in class path. – Kannan Suresh Aug 08 '11 at 07:40
0

IntentService HttpPost are probably what you want. There are plenty of snippets available on Google; here's just one of them.

Paddy
  • 2,605
  • 1
  • 18
  • 27
  • Thank you. But i did not understand where actually i have to embed the above soap request into httppost object? How actually i can place the soap request in httppost object? – Kishan Aug 02 '11 at 09:27
  • In that case, your best bet is probably a library. There's a pretty well-rounded discussion of SOAP and Android here: http://stackoverflow.com/questions/297586/how-to-call-soap-web-service-with-android – Paddy Aug 02 '11 at 09:31
  • I think i could use the answer for this stack overfolw question. [link](http://stackoverflow.com/questions/2559948/android-sending-xml-via-http-post-soap). Am i right? – Kishan Aug 02 '11 at 09:35
  • I see no reason why not. I'm not familiar enough with SOAP to help beyond the universal HTTP stuff for Android, however. – Paddy Aug 02 '11 at 09:47
  • This is more than enough. Thanks uh...! :) – Kishan Aug 02 '11 at 09:49