我正在使用 KSoap2 与服务器通信。通信正常并返回我在下面提到的一组值。我不确定我处理回复的方式。我编写了一个单独的 java 类来实现 KvmSerializable 以将 XML 对象映射到 java 对象。经过长时间的网络研究,我发现使用了像 SAX, DOM ..etc 这样的解析器更加标准和灵活。
有什么方法可以让 SAX 解析以下响应...如果是,请提供一些引用资料..
这是我从我的 Android 客户端应用程序向服务器发送请求后得到的响应。请注意,此响应未以 .xml 结尾/返回为 .xml 文件。
这是网络客户端响应:
http://134.1.10.5/Maxima/MaximaSystem.asmx/ValidateLogin
<DataSet>
<xs:schema id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" minOccurs="0"/>
<xs:element name="UserIcode" type="xs:int" minOccurs="0"/>
<xs:element name="UserUserName" type="xs:string" minOccurs="0"/>
<xs:element name="UserPassword" type="xs:string" minOccurs="0"/>
<xs:element name="UserTypeICode" type="xs:int" minOccurs="0"/>
<xs:element name="ProfileTable" type="xs:string" minOccurs="0"/>
<xs:element name="UserTypeDesc" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram>
<NewDataSet>
<Table diffgr:id="Table1" msdata:rowOrder="0">
<Name>Frecan </Name>
<UserIcode>634</UserIcode>
<UserUserName>dairy</UserUserName>
<UserPassword>dairy123</UserPassword>
<UserTypeICode>632</UserTypeICode>
<ProfileTable/>
<UserTypeDesc>Dentist</UserTypeDesc>
</Table>
</NewDataSet>
</diffgr:diffgram>
</DataSet>
这是回应。没有 XML 标签......任何东西......请建议我可以使用 Android 的 native 解析器解析它的最佳方法。如果是这样,请提供一些引用。
这是我通过 Android 客户端 ping 服务器时得到的响应示例
anyType{schema=anyType{element=anyType{complexType=anyType{choice=anyType{element=an
yType{complexType=anyType{sequence=anyType{element=anyType{....
// some values ........
};
element=anyType{};
element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{};
element=anyType{}; }; }; }; }; };unique=anyType{selector=anyType{};
field=anyType{};}; }; }; diffgram=anyType{}; }
请告诉我,是否可以在 Android 中使用 native 解析器。
提前致谢。
最佳答案
SoapObject result = (SoapObject)envelope.bodyIn;
if(result != null){
int count = result.getPropertyCount();
//TextView t = (TextView)this.findViewById(R.id.resultbox);
//t.setText("SOAP response:\n\n" + count);
SoapObject nameResult = (SoapObject) result.getProperty(0);
// TextView t = (TextView)this.findViewById(R.id.resultbox);
//t.setText("SOAP response:\n\n" + nameResult.toString());
SoapObject test = (SoapObject) nameResult.getProperty(1);
// TextView t = (TextView)this.findViewById(R.id.resultbox);
// t.setText("SOAP response:\n\n" + test.toString());
SoapObject dar = (SoapObject) test.getProperty(0);
//TextView t = (TextView)this.findViewById(R.id.resultbox);
//t.setText("SOAP response:\n\n" + dar.toString());
SoapObject suvas = (SoapObject) dar.getProperty(0);
int c = dar.getPropertyCount();
TextView t = (TextView)this.findViewById(R.id.resultbox);
t.setText("SOAP response:\n\n" + suvas.toString());
//t.setText("SOAP response:\n\n" + c);
//SoapObject nivas = (SoapObject) suvas.getProperty(NewsId);
//TextView t = (TextView)this.findViewById(R.id.resultbox);
// t.setText("SOAP response:\n\n" + nivas.toString());
}
首先,您必须在 soapObject 中获得 soap 响应,然后计算总属性。
统计完后,请一一核对您的数据在哪个特性中, 您还可以实现一个内部循环。
关于android - 哪个是在 Android 中处理 SOAP 响应的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6534089/