Thursday 13 March 2014

calling ksoap without using library string

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">SoapBasedDemo</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="progressdialog_fetch_message">Loading data...please wait!!!</string>
    <string name="url">http://213.42.55.45/CustomerServiceDataService/DataService.svc</string>
    <string name="SoaspActionURL">http://tempuri.org/IDataService/getProcedureStatus</string>
    <string name="SoapActionHeaderTitle">soapaction</string>
</resources>

calling ksoap web service without using ksoap library

package com.indianic.webservice;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;

import com.indianic.SoapBasedDemo.R;

import android.content.Context;
import android.util.Log;

public class CallSoapWebService {
    private static Context context;

    public static String getSoapResponse(Context context_param) {
        context = context_param;

        final String URL = context.getResources().getString(R.string.url);

        final String SOAP_ACTION = context.getResources().getString(R.string.SoaspActionURL);
        String resultString = null;
        String requestString = null;
        try {
            final DefaultHttpClient httpClient = new DefaultHttpClient();
            // request parameters
            HttpParams params = httpClient.getParams();
            HttpConnectionParams.setConnectionTimeout(params, 10000);
            HttpConnectionParams.setSoTimeout(params, 15000);
            // set parameter
            HttpProtocolParams.setUseExpectContinue(httpClient.getParams(),
                    true);

            // POST the envelope
            HttpPost httppost = new HttpPost(URL);
            // add headers
            httppost.setHeader(context.getResources().getString(R.string.SoapActionHeaderTitle), SOAP_ACTION);
            httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
//context.getAssets().open("request.xml");
            try {
                requestString = "<?xml version='1.0' encoding='UTF-8'?>\n"
                        + "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'\n"
                        + "xmlns:ns1='http://schemas.datacontract.org/2004/07/'\n"
                        + "xmlns:ns2='http://tempuri.org/'>\n"
                        + "<SOAP-ENV:Body>" + "<ns2:getProcedureStatus>\n"
                        + "<ns2:procedureQueryInformation>\n"
                        + "<ns1:ProcedureID>PID</ns1:ProcedureID>\n"
                        + "<ns1:ProcedureNumber>PN</ns1:ProcedureNumber>\n"
                        + "<ns1:ProcedureYear>PY</ns1:ProcedureYear>\n"
                        + "</ns2:procedureQueryInformation>\n"
                        + "</ns2:getProcedureStatus>\n" + "</SOAP-ENV:Body>\n"
                        + "</SOAP-ENV:Envelope>";
                // requestString = getXml("request.xml");
                requestString = requestString.replace("PID", "5");
                requestString = requestString.replace("PN", "1");
                requestString = requestString.replace("PY", "2014");
                Log.e("request", requestString);
                HttpEntity entity = new StringEntity(requestString);
                httppost.setEntity(entity);

                // Response handler
                ResponseHandler<String> rh = new ResponseHandler<String>() {
                    // invoked when client receives response
                    public String handleResponse(HttpResponse response)
                            throws ClientProtocolException, IOException {

                        // get response entity
                        HttpEntity entity = response.getEntity();

                        // read the response as byte array
                        StringBuffer out = new StringBuffer();
                        byte[] b = EntityUtils.toByteArray(entity);

                        // write the response byte array to a string buffer
                        out.append(new String(b, 0, b.length));
                        return out.toString();
                    }
                };

                resultString = httpClient.execute(httppost, rh);

            } catch (Exception e) {
                e.printStackTrace();
                Log.d("me", "Exc : " + e.toString());

            }

        } catch (Exception aE) {
            System.out.println(aE.toString());
            aE.printStackTrace();

        }
        return resultString;
    }

//    private static String getXml(String path) {
//
//        String xmlString = null;
//        AssetManager am = context.getAssets();
//        try {
//            InputStream is = am.open(path);
//            int length = is.available();
//            byte[] data = new byte[length];
//            is.read(data);
//            xmlString = new String(data);
//        } catch (IOException e1) {
//            e1.printStackTrace();
//        }
//
//        return xmlString;
//    }
}