在Android的解析SoapObject Responst [英] Parsing SoapObject Responst in android
问题描述
我的code是:
public class MainActivity extends Activity implements OnClickListener {
Button b;
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "GetList";
private static String SOAP_ACTION = "http://tempuri.org/IWCFMasterRole/GetList";
private static String URL = "http://172.16.0.1:55355/WCFMasterRole.svc";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.button1) {
new MyClass().execute("");
}
}
class MyClass extends AsyncTask<String, Void, SoapObject> {
SoapObject result;
@Override
protected SoapObject doInBackground(String... params) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// request.addProperty("CityId", "CITY0001");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE transport = new HttpTransportSE(URL);
try {
transport.call(SOAP_ACTION, envelope);
} catch (Exception e) {
e.printStackTrace();
}
result = (SoapObject) envelope.bodyIn;
System.out.println("Result is : " + result);
} catch (Exception e) {
System.out.println("Exception : "+e.toString());
}
return result;
}
}
}
和我的logcat的回应是:
And my response in logcat is:
Result is :
GetListResponse
{
GetListResult=anyType
{schema=anyType
{element=anyType
{complexType=anyType
{choice=anyType
{element=anyType
{complexType=anyType
{sequence=anyType
{element=anyType{}; element=anyType{}; element=anyType{};
};
};
};
};
};
};
};
diffgram=anyType
{DocumentElement=anyType
{Table1=anyType
{RoleID=ROLEAAAA0000; RoleName=Administrator; };
Table1=anyType
{RoleID=ROLEAAAA0001; RoleName=Developer; };
Table1=anyType
{RoleID=ROLEAAAA0003; RoleName=Senior Developer; };
Table1=anyType
{RoleID=ROLEAAAA0004; RoleName=Junior Developer; };
Table1=anyType
{RoleID=ROLEAAAA0005; RoleName=Trainee; };
};
};
};
}
我想获得 RoleID
和 ROLENAME
此反应。
我曾尝试与 result.getAttribute(0)
和 result.getProperty(0)
。但他们并没有帮助。此外,我用Google搜索很多。任何帮助来分析这种反应将高度appriciated。
I have tried with result.getAttribute(0)
and result.getProperty(0)
. But They didn't help. Also I have googled lot. Any help to parse this response will be highly appriciated.
感谢你。
推荐答案
Basicly这件事情是这样的:
Basicly it's something like this:
SoapObject GetListResponse = (SoapObject)result.getProperty(0);
SoapObject DocumentElement = (SoapObject)GetListResponse.getProperty(3);
SoapObject Table1 = (SoapObject)DocumentElement.getProperty(0);
这包含SoapObjects内SoapObjects,所以最好的办法是写一个递归的方法,通过所有属性进行扫描,并找到您所需要的信息。事情是这样的:
This contains SoapObjects within SoapObjects, so the best thing to do is to write a recursive method to scan through all properties and find the information you need. Something like this:
private static void ScanSoapObject(SoapObject result)
{
for(int i=0;i<result.getPropertyCount();i++)
{
if(result.getProperty(i) instanceof SoapObject)
{
ScanSoapObject((SoapObject)result.getProperty(i));
}
else
{
//do something with the current property
//get the current property name:
PropertyInfo pi = new PropertyInfo();
result.getPropertyInfo(i,pi);
String name = pi.getName();
}
}
}
这篇关于在Android的解析SoapObject Responst的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!