2012-11-14 69 views
0

我嘗試使用KSoap2從WebService獲取一些數據。KSoap2 outOfMemoryError在撥打電話時Android

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; 
} 

我也coppied的HttpTransportSE.java和操縱它直接在文件中寫入。但是我得到一個無效的令牌錯誤。

回答

0

我記得之前已經看到這個問題:

兩條建議:

1)直接保存您的SOAP XML流盤,你下載。不要將其存儲在內存中。

2)使用SAX風格的解析器解析它,其中不會將整個DOM加載到內存中,而是將其解析爲塊。

編輯:選中此 - >Very large SOAP response - Android- out of memory error

+0

我已經閱讀過。我知道我必須在磁盤上傳輸它,但我不知道如何。 – Karas

+0

從編輯中閱讀鏈接將幫助您 –

+0

我已經閱讀過它。我試過這種方式,但後來我得到了一個無意識的令牌異常。 – Karas

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,也不會出現解析錯誤。

+0

你可以請你發佈你的代碼,你是如何解析上述文件的。我是新來解析這種類型的響應在android中。 – joy