Wednesday, 19 March 2014

How to call SOAP service in android applicaiton


For Running Below code you should have following kasoap2 jar file
Ksoap jar file

public class NewClass extends Activity
{
    public final String NAMESPACE = "***";
    public final String URL = "***";
   
    public final String SOAP_ACTION_1 = "**";
    public final String METHOD_NAME_1 = "**";
   
   
   
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_1);
        /**
         * Below one line is for Passing Parameter to Soap Service.
         */
        request.addProperty("ID",3); //Parameter Pass if needed.
       
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

        try {
            androidHttpTransport.call(SOAP_ACTION_1, envelope);
       
            /**
             * Below two line for check SOAP error if you passing wrong parameter or anything else
             */
            SoapFault error = (SoapFault)envelope.bodyIn;
            System.out.println("Error message : "+error.toString());
           
           
            SoapObject mSoapObjectCompanyDetailResponse = (SoapObject)envelope.bodyIn;
           
            Object re= null;
            re = envelope.getResponse();

            Log.i("myApp", re.toString());
            System.out.println("Response "+re.toString());
            //                System.out.println("fff "+re.toString());


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

    }
}

No comments:

Post a Comment