Saurabh Verma
Saurabh Verma

Reputation: 1544

Calling Soap webservice from android

I am using the below process,

1)Create a String template for a SOAP request and substitute user-supplied values at runtime in this template to create a valid request. 2) Wrap this string in a StringEntity and set its content type as text/xml 3) Set this entity in the SOAP request.

and with the help of httppost I am posting the request,

I am using a demo webservice from w3schools.com

url--->

http://www.w3schools.com/webservices/tempconvert.asmx

What I have tried is,

HttpPost httppost = new HttpPost("http://www.w3schools.com/webservices/tempconvert.asmx");          
        StringEntity se;
        try {
            SOAPRequestXML="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\"><soapenv:Header/><soapenv:Body><tem:CelsiusToFahrenheit><!--Optional:--><tem:Celsius>30</tem:Celsius></tem:CelsiusToFahrenheit></soapenv:Body></soapenv:Envelope>";
            Log.d("request is ", SOAPRequestXML+"!!!");
            se = new StringEntity(SOAPRequestXML,HTTP.UTF_8);


        se.setContentType("text/xml");  
        httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
        httppost.setEntity(se);  

        HttpClient httpclient = new DefaultHttpClient();
        BasicHttpResponse httpResponse = 
            (BasicHttpResponse) httpclient.execute(httppost);
        HttpEntity resEntity = httpResponse.getEntity();
        t.setText(EntityUtils.toString(resEntity));



        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

I am able to get the response in soapui, so surely the code is wrong because in emulator I am getting the output,

"the server cannot service the request because the media type is unsupported".

Am I passing the correct parameter in the constructor of HttpPost or am I making the correct xml request.I tried a lot but could not figure it out.

Thanks

Upvotes: 2

Views: 11240

Answers (5)

Andrew
Andrew

Reputation: 500

This way the html form is posting 123 celsius. No SOAP or envelops, just working:)

    try {
        HttpPost httppost = new HttpPost("http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit");
        StringEntity se = new StringEntity("Celsius=123");
        httppost.setEntity(se);
        httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        HttpResponse httpResponse = new DefaultHttpClient().execute(httppost);
        HttpEntity resEntity = httpResponse.getEntity();
        return EntityUtils.toString(resEntity);
    } catch (Exception e) {
        e.printStackTrace();
        return e.getMessage();
    }

Upvotes: 1

Chintan Rathod
Chintan Rathod

Reputation: 26034

I have written an article on How to Call Web Service in Android Using SOAP at c-sharpcorner.com.

So many person get helped from that article. You can also download it and run. I will help you to understand how to use SOAP for web service.

Edit

Take a look at following links. It has complex data handling with ksoap.

Upvotes: 3

Lalit Poptani
Lalit Poptani

Reputation: 67286

The only problem with your code is you are setting Header as,

httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");

instead of,

httppost.setHeader("Content-Type", "text/xml;charset=UTF-8");

As you can see the request in the URL that is -> w3schoools, they are using,

Content-Type: text/xml; charset=utf-8

and you where not passing the same content type. So, it was giving you error as,

The server cannot service the request because the media type is unsupported.

So, just change the Header and you will get the desired response.

Upvotes: 4

CKmum
CKmum

Reputation: 641

I have a hunch that the emulator android version and the phone version are different.

But I have few suggestions. Use following:

httppost.setHeader("Accept-Charset","utf-8");
httppost.setHeader("Accept","text/xml,application/text+xml,application/soap+xml");

similarly, set content type as all of the above.

Upvotes: 1

Dan Riza
Dan Riza

Reputation: 467

Have you tried using the ksoap2 library for Android ?

you can find it here, give it a shot :

https://code.google.com/p/ksoap2-android/

Hope this helps !

Upvotes: 1

Related Questions