我尝试使用KSoap2从WebService获取一些数据。
WebService响应一个非常大的XML文件,所以当我在做HttpTransportSE.call()时,我得到一个ouOfMemory异常。
是否有可能从Soap Webservice获取剪切响应? 或者有没有办法直接将其写入设备上的文件? 这是获取数据的类:
public static SoapObject GetItemData()
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_ITEM_DATA);
request.addProperty("Company", company);
request.addProperty("SerialNumber", serialId);
itemEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
itemEnvelope.dotNet = true;
itemEnvelope.setOutputSoapObject(request);
AndroidHttpTransportSE androidHttpTransport = new AndroidHttpTransportSE(URL);
androidHttpTransport.debug = true;
Log.d("==ITEM_URL==", URL);
try
{
androidHttpTransport.call(SOAP_ACTION_ITEM_DATA, itemEnvelope);
Log.d("==ItemVerbindung==", "Verbindung aufgebaut");
}
catch (Exception e)
{
e.printStackTrace();
Log.d("==ItemVerbindung==", "HTTPCALL nicht ausgeführt");
}
try
{
itemResult = (SoapObject)itemEnvelope.getResponse();
Log.d("==ItemResponse==", "PropertyCount: "+itemResult.getPropertyCount());
}
catch(ClassCastException e)
{
itemResult = (SoapObject)itemEnvelope.bodyIn;
}
catch (SoapFault e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if(itemResult != null)
{
return itemResult;
}
return null;
}
我还修改了HttpTransportSE.java并将其操作直接写入文件。但是我得到了一个无效的令牌错误。
答案 0 :(得分:0)
我记得以前见过这个问题:
两项建议:
1)在下载时将SOAP XML流直接保存到磁盘。不要将其存储在内存中。
2)使用 SAX风格的解析器解析它,你不会在内存中加载整个DOM,而是以块的形式解析它。
编辑:检查 - > Very large SOAP response - Android- out of memory error
答案 1 :(得分:0)
我找到了一个不使用KSoap2库的解决方案。
以下是代码:
try {
java.net.URL url = new java.net.URL(URL);
HttpURLConnection rc = (HttpURLConnection) url.openConnection();
rc.setRequestMethod("POST");
rc.setDoOutput(true);
rc.setDoInput(true);
rc.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
rc.addRequestProperty("User-Agent", HTTP.USER_AGENT);
rc.setRequestProperty("SOAPAction", SOAP_ACTION_ITEM_DATA);
OutputStream out = rc.getOutputStream();
Writer wout;
wout = new OutputStreamWriter(out);
wout.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
wout.write("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
wout.write("<soap:Body>");
wout.write("<GetItemData2 xmlns=\"http://localhost/HSWebBL\">");
wout.write("<Company>" + company + "</Company>");
wout.write("<SerialNumber>" + serialId + "</SerialNumber>");
wout.write("</GetItemData2>");
wout.write("</soap:Body>");
wout.write("</soap:Envelope>");
wout.flush();
wout.close();
rc.connect();
Log.d("==CLIENT==", "responsecode: " + rc.getResponseCode() + " " + rc.getResponseMessage());
InputStream in = new BufferedInputStream(rc.getInputStream(), BUFFER_SIZE);
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我使用SAXParser来解析InputStream。 这样我就不会得到outOfMemoryException并且不再出现解析错误。