0

我尝试使用 KSoap2 从 WebService 获取一些数据。

WebService 响应一个非常大的 XML 文件,所以当我执行 HttpTransportSE.call() 时,我得到一个 ouOfMemory 异常。

是否有可能从 Soap Web 服务中获得截断的响应?或者有没有办法将它直接写入设备上的文件中?这是我获取数据的课程:

    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 并对其进行操作以直接写入文件。但是我得到一个无效的令牌错误。

4

2 回答 2

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 并且没有解析错误了。

于 2012-11-15T14:20:40.540 回答
0

我记得以前见过这个问题:

两个建议:

1) 下载时将 SOAP XML 流直接保存到磁盘。不要将其存储在内存中。

2) 使用SAX 样式的解析器对其进行解析,您不会将整个 DOM 加载到内存中,而是将其解析为块。

编辑:检查这个->非常大的SOAP响应-Android-内存不足错误

于 2012-11-14T11:14:40.110 回答