I try to make my first android app ! I call a webservice with ksoap2 to get details of my account. I have a function witch return a Array (resultRequestSOAP). In the resultRequestSOAP there is an ARRAY of object. Below my function :
public Array listall(String session){
final String SOAP_ACTION = "http://www.nubio.net/soap/vps#listAll";
final String METHOD_NAME = "listAll";
final String NAMESPACE = "http://www.nubio.net/soap/vps";
final String URL = "http://www.nubio.net/soap/vps";
Array myArray = null;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//SoapObject
request.addProperty("session",session);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE开发者_运维问答(URL);
try
{
androidHttpTransport.call(SOAP_ACTION, envelope);
resultRequestSOAP = envelope.getResponse();
//retour = resultRequestSOAP.toString();
if(resultRequestSOAP != null){
myArray = (Array)resultRequestSOAP;
}
}
catch (Exception aE)
{
aE.printStackTrace ();;
}
return myArray;
}
I test the function to return string and it's works fine, but i need to show the array on the screen. How can i display the array in the resultRequestSOAP; ? But the original return of the soap in resultRequestSOAP; is :
array(
0 => Object{
vps_id : int
ip : string
hostname : string
password : string (optional)
os : string
os_arch : integer
os_distri : string
expire : string (DATE TIME)
},
...
)
So i can i return the array from the soap and display it!? I'm sorry for my english, i hope you could help me :) The best for me will only display the "hostname string" from the array as button is it possible?
Not really sure what your asking, but if you method returns an array of objects you can print that object out like this:
for(int i=0; i < myArray.length; i++){
Object item = myArray[i];
// Put into a TextView here if you wish, just printing to console
System.out.println("Item: "+i +" IP: "+item.ip);
// or if it is private with a getter
System.out.println("Item: "+i +" IP: "+item.getIp());
System.out.println("Hostname: "+item.hostname);
System.out.println("Hostname: "+item.getHostname());
}
I think that gives you the idea anyway?
this blog post might help: http://seesharpgears.blogspot.com/2010/10/web-service-that-returns-array-of.html
see the last method.
edit:
instead of casting to array your resultRequestSOAP
, you should get the number of properties with resultRequestSOAP.getPropertyCount()
, and cycle through them to get each object:
SoapObject pii = (SoapObject)resultRequestSOAP.getProperty(i);
from these objects you can extract their own properties (vps_id, ip, hostname...) with:
vps_id = Integer.parseInt(pii.getProperty(0).toString());
and so on. in the example they're building an array of their Category objects, but of course you can use the data like you want.
精彩评论