0

Is it possible for Android KSoap2 to pass values to webservice methods with Paramater Arrays? If yes, how?

For VB:
Public Function calcSum(ByVal ParamArray args() As Double) As Double For C#
public static Double calcSum(params int[] args)

Should it work with normal passing of parameters like this?

SoapObject request = new SoapObject(NAMESPACE + "/", methodCalcSumService);
request.addProperty("args", intArg1);
request.addProperty("args", intArg1); //2nd parameter, will this be overriden?
envelope.setOutputSoapObject(request);
androidHttpTransport.call(soapCalcSumService, envelope);
Object result = envelope.getResponse(); //Get XML Result

Thank you :)

heychar
  • 93
  • 1
  • 9

1 Answers1

0

I need to send String[] to a VB Web Service method

<WebMethod()> _
Public Function SendByteArrayString(ByVal ImageNo As String, ByVal Name As String, ByVal ParamArray ArrImage() As String) As Boolean

And this code worked for me. Hope this helps!

    /** This class serialized String Array for passing PropertyInfo to Web Service 
     *  Got this from hasanghaforian's answer (http://stackoverflow.com/questions/7130862/how-to-pass-string-array-to-webservice-using-ksoap2)
     */
    public class SerializableStringArray extends Vector<String> implements KvmSerializable
    {       
        private static final long serialVersionUID = -1166006770093411055L;
        private int intPropertyCount = 1;

        @Override
        public Object getProperty(int arg0)
        {
            return this.get(arg0);
        }

        @Override
        public int getPropertyCount()
        {
            return intPropertyCount;
        }

        @Override
        public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2)
        {
            arg2.name = "string";
            arg2.type = PropertyInfo.STRING_CLASS;
        }

        @Override
        public void setProperty(int arg0, Object arg1)
        {
            this.add(arg1.toString());
        }

        @Override
        public void setSize(int arg0)
        {
            intPropertyCount = arg0;
        }        
    }

    public final Boolean SendByteArrayString(String strServiceCallNo, String DBName, Bitmap[] bmp)
    {
        soapSendByteArrayString = NAMESPACE + "/" + methodSendByteArrayString;

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, 60000);
        boolean isValid = false;
        try
        {
            isSuccessful = false;
            SoapObject request = new SoapObject(NAMESPACE + "/", methodSendByteArrayString);
            request.addProperty("ImageNo", strServiceCallNo);
            request.addProperty("Name", strDBName);

            SerializableStringArray serializedStringArray = new SerializableStringArray();

            for (int i = 0; i < bmp.length; i++)
            {
                try
                {
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bmp[i].compress(Bitmap.CompressFormat.PNG, 100, stream);
                    byte[] byteArray = stream.toByteArray();

                    serializedStringArray.add(new String(Base64.encodeBase64(byteArray, false), "UTF-8"));
                    BusinessRules.appendLog("[ArrImage_" + i + " ]" + new String(Base64.encodeBase64(byteArray, false), "UTF-8"));
                }
                catch (Exception e)
                {
                    e.toString();
                }
            }
            serializedStringArray.setSize(bmp.length);

            PropertyInfo propertyInfoStringArray = new PropertyInfo();
            propertyInfoStringArray.setName("ArrImage");
            propertyInfoStringArray.setValue(serializedStringArray);
            propertyInfoStringArray.setType(String[].class); //Pass as a String[] class 

            request.addProperty(propertyInfoStringArray);
            envelope.setOutputSoapObject(request);

            Object result = "false";
            try
            {
                envelope.addMapping(NAMESPACE, "ArrImage", new SerializableStringArray().getClass());
                androidHttpTransport.call(soapSendByteArrayString, envelope);
                result = envelope.getResponse(); //Get XML Result
            }
            catch (Exception e)
            {
                e.printStackTrace();
                BusinessRules.appendLog("[WebService.java + SendByteArrayString +  envelope.getResponse() ]" + e.toString());
            }
            isValid = Boolean.valueOf(result.toString());
        }
        catch (Exception e)
        {
            isValid = false;
            e.printStackTrace();
            BusinessRules.appendLog("[WebService.java + SendByteArrayString ]" + e.toString());
        }

        return isValid;
    }
heychar
  • 93
  • 1
  • 9