Thursday, 18 December 2014

how to Creating SOAP Parsar in android example


/** Call this Method **/
WebserviceCall com = new WebserviceCall();
SoapResponse = com.getConvertedWeight("enter your method name");

/** Creating SOAP Parsar Method **/
    class WebserviceCall {
        /** Variable Decleration **/

        String namespace = "http://testing.org/";
        private String url = "http://testing/demo/details.asmx";
        String SOAP_ACTION;
        SoapObject request = null, objMessages = null;
        SoapSerializationEnvelope envelope;       
        AndroidHttpTransport androidHttpTransport;
        WebserviceCall() {
        }
        /** Set Envelope */       
        protected void SetEnvelope() {
            try {
                // Creating SOAP envelope
                envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                // You can comment that line if your web service is not .NET
                // one.
                envelope.dotNet = true;
                envelope.setOutputSoapObject(request);
                androidHttpTransport = new AndroidHttpTransport(url);
                androidHttpTransport.debug = true;
            } catch (Exception e) {
                System.out.println("Soap Exception---->" + e.toString());
            }
        }

        // MethodName variable is define for which webservice function will call
        public String getConvertedWeight(String MethodName) {

            try {
                SOAP_ACTION = namespace + MethodName;
                // Adding values to request object
                request = new SoapObject(namespace, MethodName);
                // Adding Double value to request object
                    PropertyInfo weightProp = new PropertyInfo();
                    weightProp.setName("id");
                    weightProp.setValue(0);
                    weightProp.setType(int.class);
                    request.addProperty(weightProp);
                    request.addProperty("Username", "testing");
                    request.addProperty("Password", "1234567");
                 }             
                SetEnvelope();

                try {
                    // SOAP calling webservice
                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    // Got Webservice response
                    String result = envelope.getResponse().toString();
                   
                    System.out.println("result----->"+result);
                  return result;

                } catch (Exception e) {
                    // TODO: handle exception
                    return e.toString();
                }
            } catch (Exception e) {
                // TODO: handle exception
                return e.toString();
            }

        }
    }


 Note :
//download jar file for soap parsar
Name for jar file -ksoap2-android-assembly-2.5.8-jar-with-dependencies

No comments:

Post a Comment