Thursday, June 28, 2012

Calling SOAP WebService using HTTPPOST and getting response in XML.

Today I am going to show you how we can call soap web-service using HTTP-POST. As we know for calling SOAP web-service you require ksoap jar to be added to your project and you can call it. But, sometimes when you have complex response from SOAP web-services its very difficult to parse.So, my motive in this post is to call the SOAP web-service and get the response in XML rather than getting response in SOAP. Though you can get the request by using "requestDump" & response in XML by using "responseDump" as

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, timeout);
androidHttpTransport.debug=true;
androidHttpTransport.call(SOAP_ACTION, envelope);
String requestString = androidHttpTransport.requestDump;
Log.d("Request in XML", requestString);
String response = androidHttpTransport.responseDump;
Log.d("Response in XML", response);

So, what you can do is create a file with an Request and keep it inside assets folder of your project. Also you have to add %s in place of paramters if you want to pass dynamic parameters to your web-service. I am going to use a live web-service from www.w3schools.com so that you can test the source code directly if you don't have your own web-service.


I am going to use a web-service that will convert Celcius to Fahrenheit. Below is my request.xml in assets folder.

<?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>
<CelsiusToFahrenheit xmlns="http://tempuri.org/">
<Celsius>%s</Celsius>
</CelsiusToFahrenheit>
</soap:Body>
</soap:Envelope>

You can see I am using %s in place of Celcius value because I am going to pass the parameter dynamically from the code not a static one. Now we have to read the xml from assets folder and pass parameter to it dynamically. Below is how you can read the xml from assets folder and convert it to String.

public static String convertStreamToString(InputStream is) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line+"\n");
    }
    is.close();
    return sb.toString();
}

You can just pass InputStream to this method and get the xml as response from this method. Just call it by

String xml = convertStreamToString(getAssets().open("request.xml"));

Now, you have the xml String you need to pass the parameter that is the value of Celcius, so you can do that by using String.format(String format, Object...args)
In this case you can do it as

String RequestString = String.format(xml, "12"); (If you have multiple paramters you can pass it by comma seperated)

Now you have your Request String ready to be passed to HTTPPost and get the Response. So, just pass the URL and RequestString to get the Response.
I had just created a simple method that will except the arguments as URL of our web-service and a String request that is the xml from assets folder.

public String getResponseByXML(String URL, String request) {
    HttpPost httpPost = new HttpPost(URL);
    StringEntity entity;
    String response_string = null;
    try {
        entity = new StringEntity(request, HTTP.UTF_8);
        httpPost.setHeader("Content-Type","text/xml;charset=UTF-8");
        httpPost.setEntity(entity);
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(httpPost);
        response_string = EntityUtils.toString(response.getEntity());
        Log.d("request", response_string);
        } catch (Exception e) {
        e.printStackTrace();
    }
    return response_string;
}

Call above method to get the HTTP-POST Response using the URL and Request String that we created with parameter.


private String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
String ResponseInXML = getResponseByXML(URL,  RequestString);
Log.d("ResponseInXML", ResponseInXML);

So, you will get your response in 
ResponseInXML . Then you can easily parse ResponseInXML  using any XML Parser. You can checkout for a Demo example for github.


11 comments:

  1. Thanx a lot bro for this post.
    I am trying this since last 1 to 2 days but i didn't findout any proper solution.
    But finally i got your post and my problem is solved programmatically as well as conceptually

    Once again Thank you.

    ReplyDelete
  2. Hi,

    Is this a complete example? Please help, I don't think I am getting the expected results.

    Thanks

    ReplyDelete
  3. Hi,

    I am new to this and I am trying to send some data from my android app to a remote server. Can you please advice on how to go about do this.

    ReplyDelete
  4. Hi,

    I am new to this and trying to send some data to server using Soap.can any one help me.Thanks in advance.

    ReplyDelete
  5. Hi,
    I am trying to call a webservice from android. The webservice requires 7 parameters to be passed to it. The webservice is "https://appiandev2.crawco.com/suite/webservice/processmodel/AddDocket?WSDL".
    Can anyone help me in this regard?

    ReplyDelete
    Replies
    1. SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
      Request.addProperty("key1", value1);
      Request.addProperty("key2", value2);
      Request.addProperty("key3", values3);
      ....
      Request.addProperty("key7", value7);

      Delete
  6. many thanks for this useful article..

    ReplyDelete