Search code examples
androidksoap

KSOAP ANDROID Implementation


Hi I have one Issue Related to Android K-SOAP and i am first time working with K-SOAP Android

this is the service link

here you can see the request and the response is also mentioned there, here is my code to hit the service and getting the response` package com.ksoap.net;

import org.ksoap2.SoapEnvelope;
   import org.ksoap2.serialization.PropertyInfo;
   import org.ksoap2.serialization.SoapObject;
   import org.ksoap2.serialization.SoapPrimitive;
   import org.ksoap2.serialization.SoapSerializationEnvelope;
   import org.ksoap2.transport.AndroidHttpTransport;
   import org.ksoap2.transport.HttpTransportSE;

   import android.app.Activity;
   import android.os.Bundle;
   import android.util.Log;
   import android.widget.TextView;

public class AndroidWebService extends Activity {

        private final String NAMESPACE = "http://webservices.iyogi.net/";
                                                                                          private                                                                                 final                                                                        String                                                             URL                                                  =                                          "http://sdservices.iyogi.net               /iyogi           /webservicesnonrestv5/toolbarwebservices.asmx";
        private final String SOAP_ACTION = "http://webservices.iyogi.net/GetSubscriptionByInputObject";
        private final String METHOD_NAME = "GetSubscriptionByInputObject";
        String request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                + "<soap:Envelope "
                + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
                + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
                + "xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">"
                + "<soap:Body>"
                + "<GetSubscriptionByInputObject xmlns=\"http://webservices.iyogi.net\">"
                + "<inpAccessCode>" + "<AccessCode>”2466276627755434”</AccessCode>"
                + "</inpAccessCode>" + "</GetSubscriptionByInputObject>"
                + "</soap:Body>" + "</soap:Envelope>";

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            // SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            String weight = "3700";
            String fromUnit = "Grams";
            String toUnit = "Kilograms";


            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
                    URL);

            try {
                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapObject response = (SoapObject) envelope.getResponse();
                int result = Integer.parseInt(response.getProperty(0).toString());
                System.out.println("result  is " + Integer.toString(result));
                System.out.println("res is -->>  "
                        + response.getProperty(0).toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
}`

Please help me hitting and getting the right response from this service Any help is highly appreciated


Solution

  •   final String URL = "url";
                  u = new java.net.URL(URL);
    
                 URLConnection uc = u.openConnection();
                 connection = (HttpURLConnection) uc;
    
                 connection.setDoOutput(true);
                 connection.setDoInput(true);
                 connection.setRequestProperty("SOAPAction", SOAP_ACTION);
                 connection.setRequestMethod("POST");
                 connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");
    
                 String xmldata ="xml request";
    
    
                 //System.out.println(xmldata);
                 OutputStream out = connection.getOutputStream();
    
                 Writer wout = new OutputStreamWriter(out);
    
                  wout.write(xmldata);
    
                    wout.flush();
    
                    wout.close();
                    InputStream is = connection.getInputStream();
                    MakeHttpRequest.actiovationParser(is,AndroidWebService.this);
    

    and it worked fine cheers happy coding