Search code examples
androidweb-servicesandroid-ksoap2

How to retrieve values using Ksoap2 webservice


I would like to retrieve array of values from java webservice in android. I am using ksoap 3.0 jar with dependencies.

I want to retrieve array of values as follows:

Array ( [resultStatus] => true [responseObjSize] => 12 [responseObjects] => Array ( [0] => Array ( [item] => Array ( [0] => 1 [1] => 1 ) ) [1] => Array ( [item] => Array ( [0] => 2 [1] => 0 ) ) [2] => Array ( [item] => Array ( [0] => 3 [1] => 0 ) ) [3] => Array ( [item] => Array ( [0] => 4 [1] => 0 ) ) [4] => Array ( [item] => Array ( [0] => 5 [1] => 0 ) ) [5] => Array ( [item] => Array ( [0] => 6 [1] => 0 ) ) [6] => Array ( [item] => Array ( [0] => 7 [1] => 0 ) ) [7] => Array ( [item] => Array ( [0] => 8 [1] => 0 ) ) [8] => Array ( [item] => Array ( [0] => 9 [1] => 0 ) ) [9] => Array ( [item] => Array ( [0] => 10 [1] => 0 ) ) [10] => Array ( [item] => Array ( [0] => 11 [1] => 0 ) ) [11] => Array ( [item] => Array ( [0] => 12 [1] => 0 ) ) ) )

Can anyone help me out.


Solution

  • public static final String NAMESPACE = "http://tempuri.org/";

     public static String GetColor(Context c) throws IOException,
                XmlPullParserException {
            String METHOD_NAME = "GetColor";
            String SOAP_ACTION = "http://tempuri.org/youruri name/";
    
            SOAP_ACTION = SOAP_ACTION + METHOD_NAME;
            SoapObject request = new SoapObject(NAMESPACE,
                    METHOD_NAME);
            // Use this to add parameters
    
            // Declare the version of the SOAP request
            return WebCalls.call(c, request, .NAMESPACE, METHOD_NAME,
                    SOAP_ACTION);
        } 
    
    
    
    
    
    
    public static String call(Context c, SoapObject request, String NAMESPACE,
                    String METHOD_NAME, String SOAP_ACTION) throws IOException,
                    XmlPullParserException {
                Log.i(WebCalls, "URL " + your URL);
                Log.i(WebCalls, "Method Name " + METHOD_NAME);
                Log.i(WebCalls, "request Name " + request);
                String SoapResult = null;
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
    
                envelope.setOutputSoapObject(request);
    
                envelope.dotNet = true;
    
                HttpTransportSE androidHttpTransport = new HttpTransportSE(
                        CommonVariable.URL);
    
                // this is the actual part that will call the webservice
                androidHttpTransport.call(SOAP_ACTION, envelope);
    
                // Get the SoapResult from the envelope body.
                if (envelope.bodyIn instanceof SoapFault) {
                    SoapResult = ((SoapFault) envelope.bodyIn).faultstring;
                } else {
                    SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
                    SoapResult = resultsRequestSOAP.getProperty(0).toString();
                }
    
                Log.i(WebCalls, "Response " + SoapResult);
    
                return SoapResult;
            }
    

    i hope this is useful method too you...if any query then tell me..this code have implemented in my current project.now its working fine.