Android - Deserializing Large Json String from webservice (java.lang.OutOfMemoryError)

First of all, I must mention that I have seen other questions asked by others about this error, but I could not figure it out.

I am making a photo gallery for my application, the photos are inserted into the site database as bytes. I have prepared a web service that returns these pictures as json string. everything was fine until the number of photos from the website database increased (the returned json string got bigger and bigger)

when i try to read this json and put it in a string i get this error:

java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.OutOfMemoryError
            at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
            at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:124)
            at java.lang.StringBuilder.append(StringBuilder.java:271)
            at org.kxml2.io.KXmlParser.readValue(KXmlParser.java:1345)
            at org.kxml2.io.KXmlParser.next(KXmlParser.java:390)
            at org.kxml2.io.KXmlParser.next(KXmlParser.java:310)
            at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:210)
            at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:366)
            at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:233)
            at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:366)
            at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:121)
            at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:137)
            at org.ksoap2.transport.Transport.parseResponse(Transport.java:63)
            at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:100)
            at ir.pargansystem.orkidehtalaei.OrkidehWebService.Call(OrkidehWebService.java:42)
            at ir.pargansystem.orkidehtalaei.photogallery.PhotoGalleryViewPager$GetDataTask.doInBackground(PhotoGalleryViewPager.java:226)
            at ir.pargansystem.orkidehtalaei.photogallery.PhotoGalleryViewPager$GetDataTask.doInBackground(PhotoGalleryViewPager.java:218)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

      

how can i read json and deserialize it without running out of memory? any help would be appreciated!

edit 1: web service code:

public String Call(String methodName) {
        String loginUser = "user";
        String loginPassword = "pass";

        SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, methodName);
        PropertyInfo pi = new PropertyInfo();
        pi.setName("username");
        pi.setValue(loginUser);
        pi.setType(String.class);
        request.addProperty(pi);
        pi = new PropertyInfo();
        pi.setName("password");
        pi.setValue(loginPassword);
        pi.setType(String.class);
        request.addProperty(pi);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;

        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION + methodName, envelope);
            response = envelope.getResponse();
        } catch (Exception exception) {
            response = exception.toString();
            exception.printStackTrace();
        }
        return response.toString();
    }

      

and calling the web service here:

private class GetDataTask extends AsyncTask<Void, Void, Void> {
        String jsonString;

        @Override
        protected Void doInBackground(Void... params) {
            // Simulates a background job.
            try {
            OrkidehWebService fws = new OrkidehWebService();
            jsonString = fws.Call("getGalleryData"); // I get the error here

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
@Override
    protected void onPostExecute(Void result) {
        try {
            Gson gson = new Gson();
            @SuppressWarnings("serial")
            Type collectionType = new TypeToken<List<PhotoGalleryEntity>>() {
            }.getType();

            lstPhotos = gson.fromJson(jsonString, collectionType);

            Collections.sort(lstPhotos, new Comparator<PhotoGalleryEntity>() {
                public int compare(PhotoGalleryEntity o1, PhotoGalleryEntity o2) {
                    return o1.getPriority() - o2.getPriority();
                }
            });
}

      

+3
java json android web-services heap-memory


source to share


No one has answered this question yet

Check out similar questions:

1818
How to get enum value from string value in Java?
1153
How do I get ASP.NET Web API to return JSON instead of XML using Chrome?
five
Can't create a handler inside a thread that didn't call Looper.prepare () 3
3
Permission denied - INTERNET permission missing?
1
'int org.ksoap2.serialization.KvmSerializable.getPropertyCount ()' for a null object reference
0
Android web service ksoap2 returns false
0
Parsing Json Object using soap in android
0
Android Studio crashes after installing camera in real device (virtual emulator works fine)
0
Json error in logcat file in aysnc task
-2
There is no class in android studio



All Articles
Loading...
X
Show
Funny
Dev
Pics