使用javax.json.JsonReader

时间:2016-01-15 16:37:08

标签: android json

我认为通过使用JsonReader而不是StringBuilder,它可以解决我的Out Of Memory错误,但似乎并非如此。

Logcat指出错误来自此行:

resultFromWeb = hf.retrieveGuestData(hf.urlWebService, postParameters);

所以这是我的retrieveGuestData

public JsonObject retrieveGuestData(String url, List<NameValuePair> nameValuePairs){
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        HttpResponse response = null;
        JsonObject finalResult = null;

        HttpEntity entity;
        InputStream inputStream;
        try {

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            response = httpclient.execute(httppost);
            entity = response.getEntity();
            inputStream = entity.getContent();
            JsonReader reader = Json.createReader(inputStream);
            finalResult = reader.readObject();

            return finalResult;

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }

        return finalResult;
    }

这是我的logcat消息:

 E/dalvikvm-heap: Out of memory on a 168-byte allocation.
 I/dalvikvm: "AsyncTask #3" prio=5 tid=14 RUNNABLE
 I/dalvikvm:   | group="main" sCount=0 dsCount=0 obj=0x2bc34380 self=0x450a80
 I/dalvikvm:   | sysTid=23441 nice=10 sched=3/0 cgrp=[fopen-error:2] handle=4605968
 I/dalvikvm:   | schedstat=( 0 0 0 ) utm=1059 stm=27 core=0
 I/dalvikvm:     at java.lang.Throwable.nativeFillInStackTrace(Native Method)
 I/dalvikvm:     at java.lang.Throwable.fillInStackTrace(Throwable.java:160)
 I/dalvikvm:     at java.lang.Throwable.<init>(Throwable.java:83)
 I/dalvikvm:     at java.lang.Error.<init>(Error.java:37)
 I/dalvikvm:     at java.lang.VirtualMachineError.<init>(VirtualMachineError.java:35)
 I/dalvikvm:     at java.lang.OutOfMemoryError.<init>(OutOfMemoryError.java:33)
 I/dalvikvm:     at java.util.HashMap.makeTable(HashMap.java:-1)
 I/dalvikvm:     at java.util.HashMap.doubleCapacity(HashMap.java:575)
 I/dalvikvm:     at java.util.HashMap.put(HashMap.java:405)
 I/dalvikvm:     at org.glassfish.json.JsonObjectBuilderImpl.putValueMap(JsonObjectBuilderImpl.java:153)
 I/dalvikvm:     at org.glassfish.json.JsonObjectBuilderImpl.add(JsonObjectBuilderImpl.java:75)
 I/dalvikvm:     at org.glassfish.json.JsonReaderImpl.readObject(JsonReaderImpl.java:191)
 I/dalvikvm:     at org.glassfish.json.JsonReaderImpl.readArray(JsonReaderImpl.java:143)
 I/dalvikvm:     at org.glassfish.json.JsonReaderImpl.readObject(JsonReaderImpl.java:180)
 I/dalvikvm:     at org.glassfish.json.JsonReaderImpl.readObject(JsonReaderImpl.java:103)
 I/dalvikvm:     at com.evenesis.imin.evenesis.HelperFunctions.retrieveGuestData(HelperFunctions.java:121)
 I/dalvikvm:     at com.evenesis.imin.evenesis.EventList$RetrieveGuestData.doInBackground(EventList.java:296)
 I/dalvikvm:     at com.evenesis.imin.evenesis.EventList$RetrieveGuestData.doInBackground(EventList.java:265)
 I/dalvikvm:     at android.os.AsyncTask$2.call(AsyncTask.java:264)
 I/dalvikvm:     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 I/dalvikvm:     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 I/dalvikvm:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
 I/dalvikvm:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
 I/dalvikvm:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
 I/dalvikvm:     at java.lang.Thread.run(Thread.java:856)
 W/dalvikvm: Exception thrown (Ljava/lang/OutOfMemoryError;) while throwing internal exception (Ljava/lang/OutOfMemoryError;)
 W/dalvikvm: Could not resolve class ref'ed in exception catch list (class index 2496, exception Ljava/lang/OutOfMemoryError;)
 I/dalvikvm-heap: Clamp target GC heap from 49.925MB to 48.000MB
 D/dalvikvm: GC_CONCURRENT freed <1K, 1% free 48990K/49159K, paused 3ms+6ms
 D/dalvikvm: GC_FOR_ALLOC freed 41450K, 85% free 7540K/49159K, paused 364ms
 W/dalvikvm: threadid=14: thread exiting with uncaught exception (group=0x2b542210)
 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
                   java.lang.RuntimeException: An error occured while executing doInBackground()
                       at android.os.AsyncTask$3.done(AsyncTask.java:278)
                       at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
                       at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
                       at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
                       at java.util.concurrent.FutureTask.run(FutureTask.java:137)
                       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
                       at java.lang.Thread.run(Thread.java:856)
                    Caused by: java.lang.OutOfMemoryError: [memory exhausted]
                       at dalvik.system.NativeStart.main(Native Method)
 D/OpenGLRenderer: Flushing caches (mode 0)

1 个答案:

答案 0 :(得分:0)

仅使用JsonReader无法解决您的问题。您需要查找close和ending标签以从中读取对象并使用它。但是如果你将响应写入文件然后从那里以块的形式读取它会更好。

希望它能给你一些开始的想法。