0

I try to get some Data from a WebService using KSoap2.

The WebService responses a very large XML-File so while I'm doing the HttpTransportSE.call() I get an ouOfMemory Exception.

Is it possible to get a snipped response from a Soap Webservice? Or is there a way to write it directly in a file on the device? This is my Class to get the Data:

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

I have also coppied the HttpTransportSE.java and manipulated it to write directly in a file. But there I get an unvalid Token error.

Karas
  • 31
  • 7

2 Answers2

0

I remember to have seen this problem before:

Two Recommendations:

1) Save your SOAP XML stream directly to disk as you download it. Don't store it in memory.

2) Parse it using a SAX-style parser, where you don't load the whole DOM in memory, but rather parse it in chunks.

EDIT: Check this -> Very large SOAP response - Android- out of memory error

Community
  • 1
  • 1
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
0

I found a solution without using the KSoap2 Library.

Here is the code:

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

I use a SAXParser to parse the InputStream. In that way I don't get an outOfMemoryException and no parsing error anymore.

dream3r
  • 11
  • 5
Karas
  • 31
  • 7
  • can you please post your code of how did you parse the above file.I am new to parsing this type of response in android. – Raj Jul 03 '13 at 06:38