嗨,我正在开发客户端/服务器应用程序。而且我能够以 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>
我想在哪里取出并只显示“ServerStatus”,即eiter Active、Inactive等。我见过很多技术,比如pullparser,但他们有自己的Http连接方法,我不想使用。所以现在告诉我该怎么做。请帮忙
我得到响应并在文本字段中显示它就像
<NewDataSet>
<Table1>
<ServerStatus>Standby</ServerStatus>
</Table1>
</NewDataSet>