2012-07-02 80 views
0

我有一個問題,即我從JSON響應接收數據,數據中也有很多圖像。有大量數據超過100個。因此,當我嘗試在LazyAdapter中加載它時,或嘗試將方法調用爲LazyAdapter的NotifydatasetChanged時,它會返回OutOfmemory異常。請提出任何解決方案。在Android應用程序中發生OutOfMemory異常

代碼:

final ProgressDialog dialog = ProgressDialog.show(
        ResearchList.this, "Research List  ", 
        "Please wait... ", true); 

      final Handler handler = new Handler() { 

       public void handleMessage(Message msg) { 

        // System.out.println("The id after Save:"+id.get(0).toString()); 

        // catagory.addAll(keyword_vector1); 
        linear_Category_Child.setVisibility(View.GONE); 
        linear_Category_Child_Child.setVisibility(View.VISIBLE); 
        //tv_Child_Header.setText("Volvo"); 
        tv_CategoryChildHeader.setText(from); 
        setHeaderImage(tv_CategoryChildHeader.getText().toString()); 

        if(coll.getDisplayNames().size()!=0) 
        { 
         adapter = new CategoryListLazyAdapter(
           ResearchList.this); 
         lvCategory.setAdapter(adapter); 
         Utility util = new Utility(); 
         util.setListViewHeightBasedOnChildren(lvCategory); 

        } 
        else 
        { 
         /*lvCategory.invalidate(); 
         lvCategory.setAdapter(null);*/ 
         AlertDialog.Builder builder = new Builder(ResearchList.this); 
         builder.setTitle("Attention!"); 
         builder.setMessage("No Data Available for the Particular Search."); 
         builder.setPositiveButton("OK", 
           new DialogInterface.OnClickListener() { 

            @Override 
            public void onClick(DialogInterface dialog, int which) { 
             // TODO Auto-generated method stub 

            } 
           }); 

         builder.create().show(); 
        } 
        /*Utility util = new Utility(); 
        util.setListViewHeightBasedOnChildren(lvCategory);*/ 
        dialog.dismiss(); 
       } 
      }; 

      final Thread checkUpdate = new Thread() { 

       public void run() { 
        try { 
         String sortEncode = URLEncoder.encode("mpg"); 
         String filterEncode = URLEncoder.encode(filter); 
         String clientEncode = URLEncoder.encode("10030812"); 
         String fromEncode = URLEncoder.encode(from); 
         String toEncode = URLEncoder.encode(to); 
         String catUrl = "/v1/vehicles/get-make-models.json?sort=" 
           + sortEncode 
           + "&filter=" 
           + filterEncode 
           + "&client-id=" 
           + clientEncode 
           + "&from=" 
           + fromEncode; 
         genSig = new GetSignature(catUrl, "acura"); 
         try { 
          signature = genSig.getUrlFromString(); 
         } catch (InvalidKeyException e1) { 
          // TODO Auto-generated catch block 
          e1.printStackTrace(); 
         } catch (NoSuchAlgorithmException e1) { 
          // TODO Auto-generated catch block 
          e1.printStackTrace(); 
         } 
         // jsonString = 
         // getJsonSring("http://api.highgearmedia.com/v1/vehicles/get-models.json?make=acura&client-id=10030812&signature=LWQbdAlJVxlXZ1VO2mfqAA=="); 
         // String signatureEncode = 
         // URLEncoder.encode(signature); 
         String urlEncode = URLEncoder.encode(catUrl 
           + "&signature=" + signature); 
         jsonString = getJsonSring("http://apibeta.highgearmedia.com" 
           + catUrl + "&signature=" + signature); 
         System.out.println("The json category:===>" 
           + jsonString); 
        } catch (ClientProtocolException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (URISyntaxException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        JsonParse json = new JsonParse(jsonString); 
        json.parseCat(); 
        LIST_SIZE = coll.getDisplayNames().size(); 
        for (int i = 0; i <= BATCH_SIZE; i++) { 
         // countriesSub.add(COUNTRIES[i]); 
         countriesSubCat.add(coll.getDisplayNames().get(i)); 
         imagesSubCat.add(coll.getImages().get(i)); 
         YearSubCat1.add(coll.getYears().get(i)); 
         YearSubCat2.add(coll.getYears().get(i + 1)); 
         mpgSubCat1.add(coll.getMpg().get(i)); 
         mpgSubCat2.add(coll.getMpg().get(i + 1)); 
         priceSubCat1.add(coll.getPrice().get(i)); 
         priceSubCat2.add(coll.getPrice().get(i + 1)); 
         ratingSubCat1.add(coll.getRating().get(i)); 
         ratingSubCat2.add(coll.getRating().get(i + 1)); 
        } 
        setLastOffset(BATCH_SIZE); 
        handler.sendEmptyMessage(0); 

       } 

      }; 
      checkUpdate.start(); 

GetJSonString方法(錯誤地):

public String getJsonSring(String api_url) throws URISyntaxException, 
      ClientProtocolException, IOException { 
     try { 
      HttpClient client = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(); 
      URL url = new URL(api_url); 
      URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), 
        url.getQuery(), null); 
      request.setURI(uri); 

      HttpResponse response = client.execute(request); 
      InputStream ips = response.getEntity().getContent(); 
      BufferedReader buf = new BufferedReader(new InputStreamReader(ips, "UTF-8")); 




      StringBuilder sb = new StringBuilder(); 
      String s; 
      while (true) { 
       s = buf.readLine(); 
       if (s == null || s.length() == 0) 
        break; 
       sb.append(s); 

      } 
      buf.close(); 
      ips.close(); 
      return sb.toString(); 

     } 

     finally { 
      // any cleanup code... 
     } 

    } 

回答

0

的問題是,你是在一次解析整個JSON飼料,這是太大,記憶中。使用JSON 解析器(如Jackson)(它也相當快),它允許您在從服務器拉取源時解析JSON對象。

當您解析圖像時,將它們轉儲到磁盤以釋放內存並在您認爲合適的時候再次加載它們。

相關問題