问题
I am calling a web service in my android app & the method is getGramaNiladhariData(), I am getting the result as a SoapObject.
result = (SoapObject) envelope.bodyIn; Log.d("WS", String.valueOf(result));
And this is what I got for String.valueOf(result)
getGramaNiladhariDataResponse{getGramaNiladhariDataResult=anyType{gnName=anyType{}; address=anyType{}; workingDays=anyType{}; gnDivision=anyType{}; contactNumber=anyType{}; }; }
Here the method i am calling returns a complexType object,consist of 5 attributes. As i found in the internet i can't get a soap Object as the result of a webservice method which return a complexType object.If so,how should i get the values.
What I want to solve is why i am getting anyType{}, as the value ,instead of the real value. Any help would be appreciated
回答1:
Its too late to answer. but FYI and others who find this useful,
By Doing String.valueOf(result)
you are printing the whole content of the body. but in order to get your values using parameters, first of all you need to hit in to correct SoapObject
.
I don't know if there is any easy way to find correct SoapObject
, but still this way do the trick, and once you get the correct SoapObject
then you are done. find below how to find the correct SoapObject
,
First you need to check the count of params in your very first SoapObject
,
result.getPropertyCount();
you will get less amount of count for this since this is the very first cover,
then, Print and see which param gives you the corect details,
result.getProperty(0);
result.getProperty(1);
etc ...
Once you found the correct parameter, then grab that SoapObject
. like this,
SoapObject result2 = (SoapObject) result.getProperty(0);
then check for the count of this object. and do the same as above until you get the correct SoapObject
.
Once you found the last SoapObject
it will print like this without useless strings,
anyType{gnName = Prasad; address = Address of the person; ; workingDays = 5; gnDivision = California; contactNumber = 0123456789}
Now you can go ahead with this object like this,
SoapObject result3 = (SoapObject) result2.getProperty(5);
Log.v("Name : ", result3.getProperty("gnName").toString());
And you will get the output in DDMS like below,
Name : Prasad
I guess this will help you, let me know if you have any further issues.
回答2:
i make own code i just finished it now i would like to share it with u may it help some
i was work with only one table and that was work with me and i will try to explain steps
try {
// Invole web service
androidHttpTransport.call(SOAP_ACTION + "mywebservicecall", envelope);
// Get the response
SoapObject resultsString = (SoapObject) envelope.getResponse();
//resultsString looks like this ==> anyType{schema=anyType{element=anyType{complexType=anyType{choice=anyT....etc
//here i found some complex in getproperty(0) so i not need it
//then i make Object1 that contain datatable from my database getproperty(1)
SoapObject Object1 = (SoapObject) resultsString.getProperty(1);
//object1 look like this ==> anyType{NewDataSet=anyType{Table1=anyType{ID_CAT=1; CAT_V_N=ma
//here my table1 i wanna to fitch the NewDataSet
SoapObject tables = (SoapObject) Object1.getProperty(0); //NewDataset
//tables object now looks like this ==> anyType{Table1=anyType{ID_CAT=1;CAT_N ...etc
//now i wanna loop in my table to get columns valus it will be tablesObject properties depend on iteration to get row by row
for (int i = 0; i < tables.getPropertyCount(); i++) {
SoapObject Objecttable = (SoapObject) tables.getProperty(i);
System.out.println("ID_CAT = " + Objecttable.getProperty("ID_CAT").toString());
System.out.println("CAT_N= " +Objecttable.getProperty("CAT_N").toString());
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("ID_CAT = 0 ");
System.out.println("CAT_N = None");
}
last code explain one table
now what i was just end is how to control multi tables
i will explain it also
SoapObject resultsString = (SoapObject) envelope.getResponse();
// the same
SoapObject Object1 = (SoapObject) resultsString.getProperty(1);
// the same
SoapObject tables = (SoapObject) Object1.getProperty(0);
// the same
// the same
for(int i = 0; i < tables.getPropertyCount(); i++){
SoapObject Objecttable = (SoapObject) (SoapObject) tables.getProperty(i);
try{
//tables.toString().substring(8,tables.toString().indexOf("=")).equals("Table1")
// here i was try to get the name of the table to hundel it but i fount my table name in attribute id
// it not came as table name like if my table that comming from database is hi
//the first row will be attrib=>id=>hi1 and sec row will be hi2
//the first row will be attrib=>rowOrder=>0 and sec row will be 1
if(Objecttable.getAttribute("id").equals("Table1"+(Integer.valueOf(Objecttable.getAttribute("rowOrder").toString())+1))){
// so Objecttable.getAttribute("id") will be ( "Table11" ) in first row
//Objecttable.getAttribute("rowOrder").toString() ==> will be 0
//Integer.valueOf(Objecttable.getAttribute("rowOrder").toString())+1) well be 1 for first row of this table
// so Objecttable.getAttribute("id").equals("Table1"+(Integer.valueOf(Objecttable.getAttribute("rowOrder").toString())+1)) will be true
/**
then i can loop and fitch data like single table
*/
}
if(Objecttable.getAttribute("id").equals("Table2"+(Integer.valueOf(Objecttable.getAttribute("rowOrder").toString())+1))){
}
}catch (Exception e) {
}
hope it's clear for you guys good luck
回答3:
anytype{} comes when you get a null value from the web service.Insert some data first and then try to fetch data from web service.
回答4:
I've had this problem before. And I solved it. I've had this problem before. And I solved it. I seemed to spend a lot of time to find a solution to this problem. My Project it's work. I create Web Service .Net Object Array. I hope this will help you.
//.................................
SoapObject requestx = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelopex = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelopex.dotNet = true;
envelopex.setOutputSoapObject(requestx);
HttpTransportSE httpTransportx = new HttpTransportSE(URL);
try {
httpTransportx.call(SOAP_ACTION, envelopex);
SoapObject responsex = (SoapObject)envelopex.getResponse(); // not envelopex.bodyIn;
int i=0;
int RCount=responsex.getPropertyCount();
int[] tbIDArray = new int[RCount+1];
int[] iMonthAarray = new int[RCount+1];
int[] iYearAarray = new int[RCount+1];
String[] sDetailAarray = new String[RCount+1];
for (i = 0; i < RCount; i++) {
Object property = responsex.getProperty(i);
if (property instanceof SoapObject) {
SoapObject info = (SoapObject) property;
String tbID = info.getProperty("tbID").toString();
String iMonth = info.getProperty("iMonth").toString();
String iYear = info.getProperty("iYear").toString();
String sDetail = info.getProperty("sDetail").toString();
tbIDArray[i] =Integer.valueOf(tbID);
iMonthAarray[i] =Integer.valueOf(iMonth);
iYearAarray[i] =Integer.valueOf(iYear);
sDetailAarray[i] =sDetail;
}//if (property instanceof SoapObject) {
}//for (i = 0; i < RCount; i++) {
} catch (Exception exception) {
MsgBox1(exception.toString() , "Error");
}
来源:https://stackoverflow.com/questions/12337137/soapobject-result-returns-anytype-as-value-when-retuning-a-complextype-object