我从.net webservice获得以下响应,它是任何格式的格式。我需要解析这种格式并读取格式中提供的RemMessage和INVM_ID等数据值。我无法解析此格式并获取适当的值.java代码是..
public class WebService {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
SoapObject request = new SoapObject(NAMESPACE, webMethName);
androidHttpTransport.debug=true;
androidHttpTransport.setXmlVersionTag("");
// Property which holds input parameters
PropertyInfo sayHelloPI = new PropertyInfo();
// Set Name
sayHelloPI.setName("UserId");
// Set Value
sayHelloPI.setValue("41");
// Set dataType
sayHelloPI.setType(String.class);
// Add the property to request object
request.addProperty(sayHelloPI);
//Set envelope as dotNet
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
try {
List<GetReminder> reminders = new ArrayList<>();
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
// Invoke web service
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get the response
String response=androidHttpTransport.responseDump;
Log.d("Result --- ", response.toString() );
SoapObject obj=(SoapObject)envelope.getResponse();
System.out.println("obj---->" + obj.toString());
} catch (SocketException e) {
Log.e("Error :" ,"Error " +e.getMessage());
//Print error
e.printStackTrace();
//Assign error message to resTxt
resTxt = "Error occured";
}
catch (XmlPullParserException e1)
{
String resp=androidHttpTransport.responseDump;
e1.printStackTrace();
resTxt = "Error occured";
}
//Return resTxt to calling object
return resTxt;
}
}
这给我回复如下,我需要读取这些参数的数据RemMessage,InvM_Id和我有GetReminder类似的
public class GetReminder implements Serializable {
private String RemMessage;
private int InvM_Id;
private int DocType;
private int PrmR_TypeId;
private int PrmR_Id;
public String getRemMessage() {
return RemMessage;
}
public void setRemMessage(String remMessage) {
RemMessage = remMessage;
}
public int getInvM_Id() {
return InvM_Id;
}
public void setInvM_Id(int invM_Id) {
InvM_Id = invM_Id;
}
public int getPrmR_TypeId() {
return PrmR_TypeId;
}
public void setPrmR_TypeId(int prmR_TypeId) {
PrmR_TypeId = prmR_TypeId;
}
public int getDocType() {
return DocType;
}
public void setDocType(int docType) {
DocType = docType;
}
public int getPrmR_Id() {
return PrmR_Id;
}
public void setPrmR_Id(int prmR_Id) {
PrmR_Id = prmR_Id;
}
}
回复格式
anyType {
schema = anyType {
element = anyType {
complexType = anyType {
choice = anyType {
element = anyType {
complexType = anyType {
sequence = anyType {
element = anyType {
};
element = anyType {
};
element = anyType {
};
element = anyType {
};
element = anyType {
};
};
};
};
};
};
};
};
diffgram = anyType {
NewDataSet = anyType {
Table = anyType {
RemMessage = ExeedDiscountLimitonInvoicedatedon05 / 03 / 2015forCSHAH,
from3 - LokhandwalaShowroom;
InvM_Id = 77693;
DocType = 3;
PrmR_TypeId = 3;
PrmR_Id = 1820;
};
};
};
}
这只是一种消息格式(RemMessage),我将获得多个RemMessage条目。
答案 0 :(得分:1)
您已在此帖子中添加了soap响应的XML表示:Parsing Complex Soap Response with diffgram。我们可以看到,第一个元素(“schema = anyType {...”)只是“NewDataSet”类型的定义。因此,您只需解析第二部分,即保存GetReminder模型的数据。 该代码位于您上次发布的https://stackoverflow.com/a/30059614/4114960
下答案 1 :(得分:0)
public ArrayList<Object> deserializeSoap(SoapObject soapObj) {
if(soapObj!=null){
if(soapObj.hasProperty("GetReminderResult")){
SoapObject SoapObj1 = (SoapObject) soapObj.getProperty("GetReminderResult");
if(soapObj1.hasProperty("schema")){
SoapObject soapObj2 = (SoapObject) soapObj1.getProperty("schema");
if(soapObj2.hasProperty("element")){
SoapObject SoapObj3 = (SoapObject) soapObj2.getProperty("element");
if(SoapObj3.hasProperty("complexType")){
SoapObject SoapObj4 = (SoapObject) SoapObj3.getProperty("complexType");
if(SoapObj4.hasProperty("choice")){
SoapObject SoapObj5 = (SoapObject) SoapObj4.getProperty("choice");
if(SoapObj5.hasProperty("element")){
SoapObject SoapObj6 = (SoapObject) SoapObj5.getProperty("element");
if(SoapObj6.hasProperty("sequence")){
SoapObject SoapObj7 = (SoapObject) SoapObj6.getProperty("sequence");
if(SoapObj7.hasProperty("element"))
{
getListFromSoapObj(SoapObj7)
}
}
}
}
}
}
}
}
}
return null;
}
private static ArrayList<Object> getListFromSoapObj(SoapObject soapObj) {
ArrayList<Object> memberObjList = new ArrayList<Object>();
for (int i = 0; i < soapObj.getPropertyCount(); i++) {
Object memberObj = new Object();
SoapObject soapObjectInner = (SoapObject) soapObj.getProperty(i);
if (soapObjectInner.hasProperty("RemMessage")) {
memberObj.setImageId(soapObjectInner
.getPropertyAsString("RemMessage"));
}
memberObjList.add(memberObj);
}
return memberObjList;
System.out.println("---------------------" + memberObjList.size());
}