0

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.

2 Answers 2

1

right now I can't think of a good example. You should examine the object that is returned. But I guess that it is a SoapObject with properties inside

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

then I guess if you call

String s = (String)result.getProperty(0);

you will get the first element of the returned array.

And ofcource, you should do the necessery error handling.

Sign up to request clarification or add additional context in comments.

Comments

0

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....

2 Comments

not exactly. With kSOAP2 you get SoapObjects, which contain attributes and properties. The rest is easy to handle.
@mihail Thanks for the input, can you please explain me or refer to a simple straight forward example for fetching the data in string array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.