gpt4 book ai didi

android - SOAP 响应不是 XML 格式

转载 作者:行者123 更新时间:2023-11-29 21:45:11 25 4
gpt4 key购买 nike

我正在开发一个使用 SOAP 网络服务的应用程序。

当我在 TextView 或 log-cat 中获得响应时,它的格式如下:

anyType{Results=anyType{Row=anyType{NAME=Demo; EMAIL=m.m@gmail.com; PHONENO=98607xxxxx; }; }; }

但是在浏览器上的响应是这样的:

<env:Envelope
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns0="http://testing.oi.com/">
<env:Body>
<ns0:getDetailsResponse>
<ns0:return>
<Results>
<Row>
<NAME>Demo</NAME>
<EMAIL>m.m@gmail.com</EMAIL>
<PHONENO>98607xxxxx</PHONENO>
</Row>
</Results>
</ns0:return>
</ns0:getDetailsResponse>
</env:Body>
</env:Envelope>

我调用 SOAP 网络服务的代码如下:

String NAMESPACE = "http://testing.oi.com/";
String URL = "http://192.168.1.xxx:8888/Testing-DemoTest-context-root/TestDemoSoapHttpPort";
String SOAP_ACTION = "http://testing.xx.com/getDetails";
String METHOD_NAME = "getDetails";

//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.implicitTypes = false;

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

try {
androidHttpTransport.debug = true;
//this is the actual part that will call the
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject)envelope.bodyIn;
//Xml.parse(result.toString(), dataHandler);
hotelDetails = result.getProperty(0).toString();
Log.d("Rsp",Details);
} catch (Exception e) {
e.printStackTrace();
}

tv.setText(hotelDetails);

我的 Java web 服务代码如下:

public class Test {
public Test() {
}
// Global Variable
Connection con;
CallableStatement cst;
String response;
ResultSet rs;
Statement stmt;

//DataBase Connection
public static Connection getConnection(){
Connection con;
con = null;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Server Connection Failed");
}

String url="jdbc:oracle:thin:@abc.def.com:PortNo:SID";
try {
con=DriverManager.getConnection(url,"UserName","Password");
if(con!=null){
System.out.println("Connection Success"+"\n");
}
} catch (SQLException e) {

System.out.println("Connection Failed");
}
return(con);
}

//XML Document Creation.
public static Document createXMLDocument(ResultSet resultset, Document doc){
ResultSetMetaData rsmd;
DocumentBuilderFactory factory;
DocumentBuilder builder;
doc = null;
Element results;
int colCount;
Connection con = getConnection();

try{
factory = DocumentBuilderFactory.newInstance();
builder= factory.newDocumentBuilder();
doc= builder.newDocument();
results = doc.createElement("Results");
doc.appendChild(results);
rsmd = resultset.getMetaData();
colCount = rsmd.getColumnCount();

while (resultset.next()){
Element row = doc.createElement("Row");
results.appendChild(row);

for (int i = 1; i <= colCount; i++){
String columnName = rsmd.getColumnName(i);
Object value = resultset.getObject(i);
if(value==null){
value=" ";
}

Element node = doc.createElement(columnName);
if(columnName.equalsIgnoreCase("BEGIN_DATE")){
String date= resultset.getString(i);
node.appendChild(doc.createTextNode(date));
row.appendChild(node);
}else{
node.appendChild(doc.createTextNode(value.toString()));
row.appendChild(node);
}
}
}
}catch (Exception e) {
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
return(doc);
}

// Call Details
@WebMethod
public Document getDetails(){
Document doc;
doc = null;
con=getConnection();
try {
cst= con.prepareCall("{call callDetails (?)}");
cst.registerOutParameter(1,OracleTypes.CURSOR);
cst.execute();
rs = (ResultSet)cst.getObject(1);
doc = createXMLDocument(rs,doc);
}catch (SQLException e) {
System.out.println("No Such Record");
}

try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
return(doc);
}

我希望响应在浏览器中以字符串形式出现,这样我就可以使用 sax 解析和解析数据。我不明白为什么我会收到这样的回复。

请指导我解决这个问题或建议我现在应该做什么。我在应用程序中间,无法移动得更远。

最佳答案

我解决了获得响应的问题。我所做的是:

我只是补充:

androidHttpTransport.debug = true;

之前:

androidHttpTransport.call(SOAP_ACTION, envelope);

在通话后我添加了:

String xml = androidHttpTransport.responseDump;

通过这个,我得到了一个字符串中的浏览器响应。

关于android - SOAP 响应不是 XML 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16055916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com