Skip to content Skip to sidebar Skip to footer

Call A Soap Webservice In Android

I need to make a call to a soap webservice in an android device. I have been reading a lot of articles in stackoverflow and on other pages, watching videos... But I've tried everyt

Solution 1:

You can do any UI related operations in doInBackGround, So move them in onPostExecute methnod.

Because doInBackGround is not a UI thread. Please read AsyncTask Document carefully. Whatever is the data you are returning from doInBackGround, it is being taken as input to onPostExecute.

So change your code as follows,

privateclassAsyncCallWSextendsAsyncTask<Void, Void, Void> {

    @OverrideprotectedvoidonPreExecute() {
        Log.i(TAG, "onPreExecute");
    }

    @OverrideprotectedString[] doInBackground(Void... params) {
        Log.i(TAG, "doInBackground");
        String[] data = sendRequest();
        return data;
    }

    @OverrideprotectedvoidonPostExecute(String[] result) {
        Log.i(TAG, "onPostExecute");
        if(result != null && result.length > 0){
             textResult.setText( results[0]);
        }
    }

}


privateString[] sendRequest(){
    SoapObject request = newSoapObject(NAMESPACE, METHOD_NAME);


    //SoapObject
    request.addProperty("@CountryName", "SPAIN");
    SoapSerializationEnvelope envelope = newSoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);


    androidHttpTransport = newHttpTransportSE(URL);
    try
    {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        resultsRequestSOAP =  envelope.getResponse();
        String[] results = (String[])  resultsRequestSOAP;
    }
    catch (Exception aE)
    {
        aE.printStackTrace ();
    }
}

Solution 2:

Have you considered making the soap requests without use of libraries? I had the same problem earlier and it led me to discovering that libraries will make your work harder, especially when it comes to making changes in the structure of the request. This is how you make a soap request without use of libraries: First of all you will need to know how to make use of SOAP Ui which is a windows application. You can import your wsdl file here and if it's syntax us correct, then you will get a screen showing the request body for your webservice. You can enter test values and you will get a response structure. This link will guide you on how to use soap ui https://www.soapui.org/soap-and-wsdl/working-with-wsdls.html

Now on to the android code :

We will create a class named runTask which extends async task and use http to send the request body and get request response :

privateclassrunTaskextendsAsyncTask<String, String, String> {

        private String response;
        Stringstring="your string parameter"StringSOAP_ACTION="your soap action here";

        StringstringUrl="http://your_url_here";
        //if you experience a problem with url remove the '?wsdl' ending@Overrideprotected String doInBackground(String... params) {

            try {

                        //paste your request structure here as the String body(copy it exactly as it is in soap ui)//assuming that this is your request bodyStringbody="<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>"+
                                "<GetCitiesByCountryResponse xmlns="http://www.webserviceX.NET">"+"<GetCitiesByCountryResult>"+string+"</GetCitiesByCountryResult>"+
                                "</GetCitiesByCountryResponse>"+
                                "</soap:Body>"+
                                "</soap:Envelope>";


                try {
                    URLurl=newURL(stringUrl);
                    HttpURLConnectionconn= (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("POST");
                    conn.setDoOutput(true);
                    conn.setDefaultUseCaches(false);
                    conn.setRequestProperty("Accept", "text/xml");
                    conn.setRequestProperty("SOAPAction", SOAP_ACTION);

                    //push the request to the server addressOutputStreamWriterwr=newOutputStreamWriter(conn.getOutputStream());
                    wr.write(body);
                    wr.flush();

                    //get the server responseBufferedReaderreader=newBufferedReader(newInputStreamReader(conn.getInputStream()));
                    StringBuilderbuilder=newStringBuilder();
                    Stringline=null;

                    while ((line = reader.readLine()) != null) {


                        builder.append(line);
                        response = builder.toString();//this is the response, parse it in onPostExecute

                    }


                } catch (Exception e) {

                    e.printStackTrace();
                } finally {

                    try {

                        reader.close();
                    } catch (Exception e) {

                        e.printStackTrace();
                    }
                }


            } catch (Exception e) {

                e.printStackTrace();
            }

            return response;
        }

        /**
         * @see AsyncTask#onPostExecute(Object)
         */@OverrideprotectedvoidonPostExecute(String result) {



           try {

              Toast.makeText(this,"Response "+ result,Toast.LENGTH_LONG).show();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

Now just execute the class and watch the magic happen:

runTaskrunner=newrunTask();
runner.execute();

You can parse the response using a DOM or SAX parser to get desired values. Feel free to ask for further clarification.

Post a Comment for "Call A Soap Webservice In Android"