gpt4 book ai didi

android - 如何将 SoapObject 强制转换或反序列化为我的对象?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:30 26 4
gpt4 key购买 nike

我有一个返回简单序列化对象的 .Net Web 服务。

我有以下代码来从我的 .Net 网络服务获得响应。如何将返回的 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));
}

}

最佳答案

我不太了解,但我认为您希望您的方法应该返回自定义类的对象而不是 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 方法。

关于android - 如何将 SoapObject 强制转换或反序列化为我的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9061155/

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