您好我正在开发一个客户端/服务器应用程序。我能够以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", "\n\n\nResponse 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>
我想在哪里取出并仅显示&#34; ServerStatus&#34; eiter Active,Inactive等我已经看过很多技术,比如pullparser但是他们有自己的Http连接方法,我不知道想用。所以现在告诉我该怎么做。请帮忙
我收到回复并在文本字段中显示它就像
<NewDataSet>
<Table1>
<ServerStatus>Standby</ServerStatus>
</Table1>
</NewDataSet>
答案 0 :(得分:0)
假设您收到服务器的回复并将其存储在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;