我有一个.Net Webservice,它返回一个简单的序列化对象。
我有以下代码来获取我的.Net webservice的响应。如何将返回的XML反序列化为我的对象?
我想返回MyObject而不是SoapPrimitive。
private static SoapPrimitive callWebServiceMethod(String url,
String namespace, String methodName,
HashMap<String, Object> parameters, String soapAction)
throws Exception {
Log.i("WebService", "URL: " + url);
Log.i("WebService", "MethodName: " + methodName);
URL myurl = new URL(url);
URLConnection connection = myurl.openConnection();
connection.setConnectTimeout(20 * 1000);
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK)
{
httpConnection.disconnect();
SoapObject request = new SoapObject(namespace, methodName);
if (parameters != null) {
String[] keys = new String[0];
keys = (String[]) parameters.keySet().toArray(keys);
Object[] vals = (Object[]) parameters.values().toArray();
for (int i = 0; i < parameters.size(); i++) {
request.addProperty(keys[i], vals[i]);
Log.i("WebService", keys[i] + ": " + vals[i]);
}
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(url,
TimeOutInSeconds * 1000);
androidHttpTransport.call(soapAction, envelope);
return (SoapPrimitive) envelope.getResponse();
}
else
{
httpConnection.disconnect();
throw new Exception(
"Http Error code: " + String.valueOf(responseCode));
}
}
答案 0 :(得分:2)
我不太了解,但我认为您希望您的方法应该返回自定义类的对象而不是SOAP对象。
所以我在类似的功能中完成了这个......
SoapObject soapObject = (SoapObject) envelope.getResponse(); // SOAP Object
realTimeUtil = new RealTimeUtil(); // CUSTOM Object of My Class
if(soapObject.hasProperty("diffgram"))
soapObject = (SoapObject) soapObject.getProperty("diffgram");
if(soapObject.hasProperty(clientName)){
soapObject = (SoapObject) soapObject.getProperty(clientName);
for (int i = 0 ; i<soapObject.getPropertyCount(); i++){
if(soapObject.hasProperty(clientName)){
SoapObject soapObject1 = (SoapObject) soapObject.getProperty(i);
if(soapObject1.hasProperty("Site")){
String site = soapObject1.getProperty("Site").toString();
String dateTime = soapObject1.getProperty("DateTime").toString();
String clicks =soapObject1.getProperty("Clicks").toString();
String orders = soapObject1.getProperty("Orders").toString();
String reveneue = soapObject1.getProperty("Revenue").toString();
String items = soapObject1.getProperty("Items").toString();
realTimeUtil.setSite(site);
realTimeUtil.setClicks(clicks);
realTimeUtil.setDateTime(dateTime);
realTimeUtil.setOrders(orders);
realTimeUtil.setRevenue(reveneue);
realTimeUtil.setItem(items);
}
}
}
}
return realTimeUtil;
RealtimeUTil是我的应用中的一个自定义类,只有很少的变量和getter-setter方法。