简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM