持有代表500个对象的json对象时,如何每次循环创建20个对象?

时间:2019-06-14 05:47:32

标签: android loops memory

我已经使用Volley库从HTTP请求中获取了500个整数数组。

数组中500个整数中的每个整数将用于创建单独的HTTP请求本身。问题在于,在遍历数组长度时,内存无法容纳500个对象,并且由于outOfMemoryException而崩溃。

这是我的代码:

private void networkRequest() {
        httpRequest = new HttpRequest(this, Global.TOP_STORIES, new HttpRequest.OnHttpCompleteListener() {
            @Override
            public void onComplete(String response) {
                createNewsList(response);

            }

            @Override
            public void onError(String error) {
                Log.i("error", error);
            }
        });
        httpRequest.request();
    }

    private void createNewsList(String response) {
        final Gson gson = new Gson();
        int[] topStories = gson.fromJson(response, int[].class);
        //TODO - fix the loop to iterate through all topStories length
        for (int i = 0; i < 20; i++) {
            String url = Global.BASE_NEWS_URL + topStories[i] + Global.NEWS_ENDPOINT;
            Log.i("url", url);

            httpRequest = new HttpRequest(this, url, new HttpRequest.OnHttpCompleteListener() {
                @Override
                public void onComplete(String response) {
                    NewsItem newsItem = gson.fromJson(response, NewsItem.class);
                    if (newsItem != null) {
                        newsItemList.add(newsItem);
                        Log.i("item", newsItem.toString());
                        newsAdapter.notifyDataSetChanged();
                    }
                    Log.i("number of news items", "number of newsItems " + newsItemList.size());
                }

                @Override
                public void onError(String error) {
                    Log.i("create list error", error);
                }
            });
            httpRequest.request();
        }
    }

如您所见,现在我仅将循环限制为仅迭代20次,这样我可以验证它是否确实有效并且确实有效。

根据我的需要,每次循环执行20个对象的SQLite注入的实现看起来如何?

此外,这是我的HttpRequest类,如果它与它有任何关系:

public class HttpRequest {

    private Context context;
    private String url;
    private OnHttpCompleteListener onHttpCompleteListener;
    private RequestQueue queue;

    public HttpRequest(Context context, String url, OnHttpCompleteListener onHttpCompleteListener) {
        this.context = context;
        this.url = url;
        this.onHttpCompleteListener = onHttpCompleteListener;
        queue = Volley.newRequestQueue(context);

    }

    public void request(){
        RequestQueue queue = Volley.newRequestQueue(context);

        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null){
                    onHttpCompleteListener.onComplete(response);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                onHttpCompleteListener.onError(error.getMessage());
            }
        });
        queue.add(stringRequest);
    }


    public interface OnHttpCompleteListener{
        void onComplete(String response);
        void onError(String error);
    }
}

0 个答案:

没有答案