Search code examples
javaandroidweb-servicesobjectksoap2

Converting Object received using SOAP from (C# .net) web service to a String array in Android


I am trying to send string array from web service to android application using kSOAP2. Using kSOAP2, I receive an response in Object format at android end. Now i want to convert this object in String Array format so that i can pass this array directly to GridView.

My Web Service Method is as follows:

[WebMethod]
public string[] getit(string status)
{
   string[] result;
   if (status == "blue")
      result = new string[] { "One", "Two", "Three", "Four" };
   else
      result = new string[] { "five", "six", "Seven", "Eight" };
   return result;
}

My Android Code is as follows:

public void call(String status)
{
   try {    
       SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

       request.addProperty("status",status.toString());

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

       HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
       androidHttpTransport.call(SOAP_ACTION, envelope);

       Object result = (Object)envelope.getResponse();

       array1=(String []) result;   //This statement does not work
   } 
}

In android code array1 is globally declared string array. String array1[]; I want to convert Object result to be converted into array1 String array. Thanks in advance.


Solution

  • first print the "result" object using result.toString().... and see what it returns and after that it will not directly convert into the string[] first it should convert into the Object[] array... and one by one it will convert into the string[] array... so your code should be like....

    Object[] objArr=(Object[])result;

    String[] strArr=new String[objArr.length];

    for(int i=0;i<=objArr.length;i++)

    {

      strArr[i]=objArr[i].toString();
    

    }

    Thats how u can do it....