android - Out of memory error while parsing a large json response -
i have large json response
. storing in string. getting out of memory error while parsing json.
here parsing this
public arraylist<string> parsing() { system.out.println("parsing start"); arraylist<string> urls=new arraylist<string>(); smusicname=null;smoviename=null; sbookname=null;stelevisionname=null; int position=0; try { jsonobject node1=new jsonobject(response); jsonarray array1=node1.getjsonarray("data"); int length=array1.length(); for(int i=0;i<length;i++){ if(array1.getjsonobject(i).getstring("id").equals(friendid.tostring())){ position=i; } } jsonobject node2 = array1.getjsonobject(position); try{ if(node2.has("music")) { jsonarray array2=node2.getjsonobject("music").getjsonarray("data"); jsonobject node4=array2.getjsonobject(0); string name=node4.getstring("name"); smusicname=name; jsonobject node5=node4.getjsonobject("picture"); jsonobject node6=node5.getjsonobject("data"); string musicsurl=node6.getstring("url"); urls.add(musicsurl); } } catch(jsonexception e) { e.printstacktrace(); } try{ if(node2.has("movies")){ jsonarray array2=node2.getjsonobject("movies").getjsonarray("data"); jsonobject node4=array2.getjsonobject(0); string name=node4.getstring("name"); smoviename=name; jsonobject node5=node4.getjsonobject("picture"); jsonobject node6=node5.getjsonobject("data"); string moviesurl=node6.getstring("url"); urls.add(moviesurl); } } catch(jsonexception e){ e.printstacktrace(); } try{ if(node2.has("books")) { jsonarray array2=node2.getjsonobject("books").getjsonarray("data"); jsonobject node4=array2.getjsonobject(0); string name=node4.getstring("name"); sbookname=name; jsonobject node5=node4.getjsonobject("picture"); jsonobject node6=node5.getjsonobject("data"); string bookurl=node6.getstring("url"); urls.add(bookurl); } } catch(jsonexception e){ e.printstacktrace(); } try{ if(node2.has("television")) { jsonarray array2=node2.getjsonobject("television").getjsonarray("data"); jsonobject node4=array2.getjsonobject(0); string name=node4.getstring("name"); stelevisionname=name; jsonobject node5=node4.getjsonobject("picture"); jsonobject node6=node5.getjsonobject("data"); string televisionsurl=node6.getstring("url"); urls.add(televisionsurl); } } catch(jsonexception e) { e.printstacktrace(); } } catch (exception e) { log.i("jsonexception", e.getmessage()); } return urls; }`
here getting out of memory error
jsonobject node1=new jsonobject(response);
where response string stored json response.
and logcat message
`07-25 09:47:21.906: e/dalvikvm-heap(1055): out of memory on 634-byte allocation. 07-25 09:47:21.906: i/dalvikvm(1055): "asynctask #9" prio=5 tid=17 runnable jit 07-25 09:47:21.906: i/dalvikvm(1055): | group="main" scount=0 dscount=0 obj=0x4101bd90 self=0x2a929320 07-25 09:47:21.916: i/dalvikvm(1055): | systid=1144 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=714125688 07-25 09:47:21.916: i/dalvikvm(1055): | state=r schedstat=( 1364254144 10263103582 272 ) utm=129 stm=7 core=0 07-25 09:47:21.916: i/dalvikvm(1055): @ java.lang.abstractstringbuilder.enlargebuffer(abstractstringbuilder.java:~94) 07-25 09:47:21.926: i/dalvikvm(1055): @ java.lang.abstractstringbuilder.append0(abstractstringbuilder.java:162) 07-25 09:47:21.926: i/dalvikvm(1055): @ java.lang.stringbuilder.append(stringbuilder.java:311) 07-25 09:47:21.926: i/dalvikvm(1055): @ org.json.jsontokener.nextstring(jsontokener.java:212) 07-25 09:47:21.926: i/dalvikvm(1055): @ org.json.jsontokener.nextvalue(jsontokener.java:107) 07-25 09:47:21.926: i/dalvikvm(1055): @ org.json.jsontokener.readobject(jsontokener.java:385) 07-25 09:47:21.926: i/dalvikvm(1055): @ org.json.jsontokener.nextvalue(jsontokener.java:100) 07-25 09:47:21.956: i/dalvikvm(1055): @ org.json.jsontokener.readobject(jsontokener.java:385) 07-25 09:47:21.956: i/dalvikvm(1055): @ org.json.jsontokener.nextvalue(jsontokener.java:100) 07-25 09:47:21.956: i/dalvikvm(1055): @ org.json.jsontokener.readobject(jsontokener.java:385) 07-25 09:47:21.966: i/dalvikvm(1055): @ org.json.jsontokener.nextvalue(jsontokener.java:100) 07-25 09:47:21.966: i/dalvikvm(1055): @ org.json.jsontokener.readarray(jsontokener.java:430) 07-25 09:47:21.966: i/dalvikvm(1055): @ org.json.jsontokener.nextvalue(jsontokener.java:103) 07-25 09:47:21.966: i/dalvikvm(1055): @ org.json.jsontokener.readobject(jsontokener.java:385) 07-25 09:47:21.966: i/dalvikvm(1055): @ org.json.jsontokener.nextvalue(jsontokener.java:100) 07-25 09:47:21.966: i/dalvikvm(1055): @ org.json.jsonobject.<init>(jsonobject.java:154) 07-25 09:47:21.966: i/dalvikvm(1055): @ org.json.jsonobject.<init>(jsonobject.java:171) 07-25 09:47:21.966: i/dalvikvm(1055): @ com.facebook.androidd.myfragment.parsing(myfragment.java:662) 07-25 09:47:21.966: i/dalvikvm(1055): @ com.facebook.androidd.myfragment$downloadimagestask1.doinbackground(myfragment.java:432) 07-25 09:47:21.966: i/dalvikvm(1055): @ com.facebook.androidd.myfragment$downloadimagestask1.doinbackground(myfragment.java:1) 07-25 09:47:21.966: i/dalvikvm(1055): @ android.os.asynctask$2.call(asynctask.java:287) 07-25 09:47:21.966: i/dalvikvm(1055): @ java.util.concurrent.futuretask.run(futuretask.java:234) 07-25 09:47:21.966: i/dalvikvm(1055): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 07-25 09:47:21.966: i/dalvikvm(1055): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 07-25 09:47:21.966: i/dalvikvm(1055): @ java.lang.thread.run(thread.java:856)`
plz me.
you shouldn't go bad fix increasing heap size. may fix problem temporarily @ point (if json gets larger can stored in memory @ all), crash again.
the proper , authentic way use gson streaming
. can google or check out this link. it's not terribly difficult either. have fun! ;)
ps: there other ways this, instance read use jackson
in place of gson
, there's jsonreader
class in android sdk (api 11+) same.
Comments
Post a Comment