嗨,我正在开发客户端/服务器应用程序。我能够以 xml 格式从服务器获得响应。但现在我必须整理或过滤数据。我正在使用以下代码从服务器获取回复,并且工作正常
package com.example.KsoapExample;
import android.util.Log;
import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import java.io.IOException;
import java.net.Proxy;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: Sashen
* Date: 12/17/13
* Time: 10:58 AM
* To change this template use File | Settings | File Templates.
*/
public class SoapRequests {
private static final boolean DEBUG_SOAP_REQUEST_RESPONSE = true;
//
private static final String MAIN_REQUEST_URL ="http://192.168.0.7/xx/service.asmx";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ACTION = "http://tempuri.org/";
private static String SESSION_ID;
public static String Toaster=null ;
private final void testHttpResponse(HttpTransportSE ht) {
ht.debug = DEBUG_SOAP_REQUEST_RESPONSE;
if (DEBUG_SOAP_REQUEST_RESPONSE) {
Log.v("SOAP RETURN", "Request XML:n" + ht.requestDump);
Log.v("SOAP RETURN", "nnnResponse XML:n" + ht.responseDump)
;
Toaster=ht.responseDump;
}
}
public String getCelsiusConversion(String fValue) {
String data = null;
String methodname = "GetServerStatus";
SoapObject request = new SoapObject(NAMESPACE, methodname);
request.addProperty("username", "admin");
request.addProperty("password", "admin");
SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
HttpTransportSE ht = getHttpTransportSE();
try {
ht.call(SOAP_ACTION + methodname, envelope);
testHttpResponse(ht);
SoapPrimitive resultsString = (SoapPrimitive)envelope.getResponse();
List<HeaderProperty> COOKIE_HEADER = (List<HeaderProperty>) ht.getServiceConnection().getResponseProperties();
for (int i = 0; i < COOKIE_HEADER.size(); i++) {
String key = COOKIE_HEADER.get(i).getKey();
String value = COOKIE_HEADER.get(i).getValue();
if (key != null && key.equalsIgnoreCase("set-cookie")) {
SoapRequests.SESSION_ID = value.trim();
Log.v("SOAP RETURNCookiee", "Cookie :" + SoapRequests.SESSION_ID);
break;
}
}
data = resultsString.toString();
} catch (SocketTimeoutException t) {
t.printStackTrace();
} catch (IOException i) {
i.printStackTrace();
} catch (Exception q) {
q.printStackTrace();
}
return data;
}
private final SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.setAddAdornments(false);
envelope.setOutputSoapObject(request);
return envelope;
}
private final HttpTransportSE getHttpTransportSE() {
HttpTransportSE ht = new HttpTransportSE(Proxy.NO_PROXY,MAIN_REQUEST_URL,60000);
ht.debug = true;
ht.setXmlVersionTag("<?xml version="1.0" encoding= "UTF-8" ?>");
return ht;
}
private final List<HeaderProperty> getHeader() {
List<HeaderProperty> header = new ArrayList<HeaderProperty>();
HeaderProperty headerPropertyObj = new HeaderProperty("cookie", SoapRequests.SESSION_ID);
header.add(headerPropertyObj);
return header;
}
}
在日志中,我可以看到服务器正在为我提供数据,并且我正在将该数据放入文本字段中,但它看起来像以下内容
<NewDataSet>
<Table>
<ServerStatus>Active</ServerStatus >
</Table>
</NewDataSet>
我想取出并仅显示"服务器状态"即活动,非活动等。我已经见过许多技术,例如拉解析器,但它们有自己的 Http 连接方法,我不想使用。所以现在告诉我该怎么做。请帮忙
我收到响应并将其显示在文本字段中,就像
<NewDataSet>
<Table1>
<ServerStatus>Standby</ServerStatus>
</Table1>
</NewDataSet>
假设您从服务器收到回复并将其存储在result
中。
String result = "<NewDataSet><Table><ServerStatus>Active</ServerStatus></Table></NewDataSet>";
String status = "error";
Document dom = null;
DocumentBuilder builder;
DocumentBuilderFactory factory;
try {
InputStream resultStream = new ByteArrayInputStream(result.getBytes("UTF-8"));
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
dom = builder.parse(resultStream);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(dom != null) {
NodeList nodeList = dom.getElementsByTagName("Table");
for (int itr = 0; itr < nodeList.getLength(); itr++) {
Node node = nodeList.item(itr);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;
status = eElement.getElementsByTagName("ServerStatus").item(0).getTextContent();
}
}
}
Log.d(TAG, status);
进口:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;