有没有在Android的解析SoapObject更好的办法 [英] Is there a better way of parsing SoapObject in Android
问题描述
到目前为止,我已经成功地连接到Web服务,并让我SoapObject回应,但我无法缩小解析一个良好的,干净的方式。搜索互联网还没有真正取得多少成果,每个人都有不同的方式和不同的过程通常对自己的Web服务面向使其成为一个一次性使用类型的解决方案。基本上,下面是响应我从Web服务获得
So far I've managed to connect to a Web Service and get my SoapObject response, but I'm having trouble narrowing down a good, clean way of parsing. Searching the Internet hasn't really yielded much results, everyone has a different way and different process usually geared towards their own Web Service making it a one time use type of solution. Basically, below is the response that I get from a Web Service
anyType{
schema=anyType{
element=anyType{
complexType=anyType{
choice=anyType{
element=anyType{
complexType=anyType{
sequence=anyType{
element=anyType{};
element=anyType{};
element=anyType{};
element=anyType{}; }; }; }; }; }; }; };
diffgram=anyType{
NewDataSet=anyType{
Rep_x0020_Information=anyType{
Login=CorrectLogin; Password=InCorrectPass; }; }; }; }
和基本上我希望能够解析出刚才的那两个重要领域(登录名和密码)。从我读我试过刚刚经历基于属性计数SoapObject响应迭代,但似乎并没有工作。当我尝试,我得到的2财产计数响应所以不是我最后做这样下面的一些事情:
And basically I want to be able to parse out just the two important fields (Login and Password). From what I read I tried just iterating through the SoapObject response based on the property count but that doesn't seem to work. When I tried I get a property count of 2 for the response so instead I ended up doing some thing like this below:
SoapObject response=(SoapObject) envelope.getResponse();
if (response != null) {
Log.i("Message", "the response contains: " + response.toString());
SoapObject diffgram = (SoapObject) response.getProperty("diffgram");
SoapObject NewDataSet = (SoapObject) diffgram.getProperty("NewDataSet");
SoapObject RepInfo = (SoapObject) NewDataSet.getProperty("Rep_x0020_Information");
for (int i = 0; i < RepInfo.getPropertyCount(); i++) {
PropertyInfo info = new PropertyInfo();
RepInfo.getPropertyInfo(i, info);
Log.d("Info", info.name + " : " + RepInfo.getProperty(i).toString());
}
//which gives the following message in LogCat
D/Info(716): Login : InCorrectLogin
D/Info(716): Password : InCorrectPass
这个过程的工作,由最后一个循环,我得到了两个对象我想,但我只是觉得有这样做的更清洁的方式。我只问,因为我再进入这个应用程序将有被做了一些更复杂的Web服务调用,我希望能够有东西在整个应用程序是,而不必建几个SoapObjects为每个请求可重复使用的只是为了让到我想要的对象。
This process works, as by the last loop I get the two objects I want but I just feel like there's a cleaner way of doing this. I only ask because as I get further into this App there will be some more complex Web Service calls being made and I want to be able to have something that is reusable throughout the app instead of having to build several SoapObjects for each request just to get down to the objects I want.
推荐答案
有2种方式,从Web服务解析对象:结果
第一,假设你是一个Web服务的所有者或创建者,可以使Web服务返回一个JSON字符串,然后从客户端,您可以创建一个实体映射到JSON和JSON库会照顾解析为你。结果
There are 2 ways to parse object from web service:
First, assume you are a web service owner or creator, you can make web service return a Json string, then from client, you can create an entity map to Json, and Json library will take care parsing for you.
二:Web服务通常应对许多复杂的对象,这样可以使实体与这些对象映射。结果
您可以按照本教程理解如何映射一个简单的对象,然后再对这个问题知道解析复杂的对象我的回答从。结果
这只是这样的事情:
Second: web service usually response many complex objects, so you can make entities to map with these objects.
You can follow this tutorial to understand how to map a simple object, then go to my answer from this question to know parsing the complex object.
It's just something like this:
HttpTransportSE androidHttpTransport = new HttpTransportSE(SERVER_URL);
androidHttpTransport.call(SOAP_ACTION, envelope);// call
Entity entity = (Entity)envelope.bodyIn;
这篇关于有没有在Android的解析SoapObject更好的办法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!