我有一個大的json response
。我將它存儲在一個字符串中。但是在解析這個json的時候,我的內存出錯了。
這裏我解析這樣解析大型json響應時出現內存不足錯誤
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;
}`
這裏我得到了內存不足的錯誤
JSONObject node1=new JSONObject(response);
其中反應是我存儲JSON響應的字符串。
,這是我的logcat消息
`07-25 09:47:21.906: E/dalvikvm-heap(1055): Out of memory on a 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): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:~94)
07-25 09:47:21.926: I/dalvikvm(1055): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:162)
07-25 09:47:21.926: I/dalvikvm(1055): at java.lang.StringBuilder.append(StringBuilder.java:311)
07-25 09:47:21.926: I/dalvikvm(1055): at org.json.JSONTokener.nextString(JSONTokener.java:212)
07-25 09:47:21.926: I/dalvikvm(1055): at org.json.JSONTokener.nextValue(JSONTokener.java:107)
07-25 09:47:21.926: I/dalvikvm(1055): at org.json.JSONTokener.readObject(JSONTokener.java:385)
07-25 09:47:21.926: I/dalvikvm(1055): at org.json.JSONTokener.nextValue(JSONTokener.java:100)
07-25 09:47:21.956: I/dalvikvm(1055): at org.json.JSONTokener.readObject(JSONTokener.java:385)
07-25 09:47:21.956: I/dalvikvm(1055): at org.json.JSONTokener.nextValue(JSONTokener.java:100)
07-25 09:47:21.956: I/dalvikvm(1055): at org.json.JSONTokener.readObject(JSONTokener.java:385)
07-25 09:47:21.966: I/dalvikvm(1055): at org.json.JSONTokener.nextValue(JSONTokener.java:100)
07-25 09:47:21.966: I/dalvikvm(1055): at org.json.JSONTokener.readArray(JSONTokener.java:430)
07-25 09:47:21.966: I/dalvikvm(1055): at org.json.JSONTokener.nextValue(JSONTokener.java:103)
07-25 09:47:21.966: I/dalvikvm(1055): at org.json.JSONTokener.readObject(JSONTokener.java:385)
07-25 09:47:21.966: I/dalvikvm(1055): at org.json.JSONTokener.nextValue(JSONTokener.java:100)
07-25 09:47:21.966: I/dalvikvm(1055): at org.json.JSONObject.<init>(JSONObject.java:154)
07-25 09:47:21.966: I/dalvikvm(1055): at org.json.JSONObject.<init>(JSONObject.java:171)
07-25 09:47:21.966: I/dalvikvm(1055): at com.facebook.androidd.MyFragment.parsing(MyFragment.java:662)
07-25 09:47:21.966: I/dalvikvm(1055): at com.facebook.androidd.MyFragment$DownloadImagesTask1.doInBackground(MyFragment.java:432)
07-25 09:47:21.966: I/dalvikvm(1055): at com.facebook.androidd.MyFragment$DownloadImagesTask1.doInBackground(MyFragment.java:1)
07-25 09:47:21.966: I/dalvikvm(1055): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-25 09:47:21.966: I/dalvikvm(1055): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-25 09:47:21.966: I/dalvikvm(1055): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-25 09:47:21.966: I/dalvikvm(1055): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-25 09:47:21.966: I/dalvikvm(1055): at java.lang.Thread.run(Thread.java:856)`
plz幫助我。
沒有任何建議的答案對您有幫助嗎? – Sufian