0

我有一个问题,我从 JSON 响应中接收数据,数据中也有很多图像。有大量数据超过 100。因此,当我尝试在 LazyAdapter 中加载它或尝试调用 LazyAdapter 的 NotifydatasetChanged 方法时,它会返回 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...
        }

    }
4

1 回答 1

0

问题是您一次解析整个 JSON 提要,它太大而无法保存在内存中。使用 JSON解析器,例如Jackson(它也非常快),它允许您在从服务器提取提要时解析 JSON 对象。

当您解析图像时,将它们转储到磁盘以释放内存并在您认为合适的时候再次加载它们。

于 2012-07-02T17:30:40.247 回答