java—获取数据时json解析速度慢

jum4pzuy  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(262)

我使用wordpressrestapi来获取数据,而使用volley一次只能获取10篇文章。
api链接
当我在浏览器中请求api时,它会在6-7秒内给出一个结果。。。
但我的应用程序需要15秒才能显示10篇帖子。
有时显示数据需要1分钟以上。
我甚至添加了一个参数,每页只能得到2篇文章。但响应时间是一样的。
我搜索和修改代码从2天,但不能摆脱它。
这是你的名字code:-

private void jsonrequest(int pageNo) {
    JSON_URL = "https://jamuitoday.com/wp-json/wp/v2/posts?_embed&page=" + pageNo;
    JsonArrayRequest request = new JsonArrayRequest(JSON_URL, response -> {

        JSONObject jsonObject;
        for (int i = 0; i < response.length(); i++) {
            try {

                isLoading = true;
                isNewsLoaded = false;
                jsonObject = response.getJSONObject(i);
                News news = new News();

                //Get News Link
                String link = jsonObject.getString("link");
                news.setLink(link);

                //Set Date
                String date = jsonObject.getString("date");
                String d = date.substring(0, 10);
                int time = Integer.parseInt(date.substring(11, 13));
                if (time > 12) {
                    String ti = date.substring(13, 16);
                    String dt = d + "     " + (time - 12) + ti + " PM";
                    news.setdate(dt);
                } else {
                    String ti = date.substring(11, 16);
                    String dt = d + "     " + ti + " AM";
                    news.setdate(dt);
                }

                //Set Title
                JSONObject title = jsonObject.getJSONObject("title");
                String titleren = title.getString("rendered");
                news.setName(titleren);

                //Set Description
                SpannableString content = new SpannableString("Content");
                content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
                JSONObject desc = jsonObject.getJSONObject("content");
                String descSub = desc.getString("rendered");
                news.setDescription(String.valueOf(Html.fromHtml((descSub))));
                news.setAdImgUrl(descSub);

                // Set Image
                JSONObject img = new JSONObject(jsonObject.getString("_embedded"));
                JSONArray ttt = img.getJSONArray("wp:featuredmedia");
                String s = "";
                for (int t = 0; t < ttt.length(); t++) {
                    JSONObject obj = ttt.getJSONObject(t);
                    s = obj.getString("source_url");
                }
                news.setImage_url(s);

                listNews.add(news);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        setuprecyclerview(listNews);
        isLoading = false;
        isNewsLoaded = true;

        pageNumber += 1;

        if (recyclerView.getLayoutManager() != null) {
            recyclerView.getLayoutManager().scrollToPosition(currentListPosition);
        }

        if (shimmerFrameLayout.isShimmerVisible()) {
            shimmerFrameLayout.stopShimmer();
            shimmerFrameLayout.setVisibility(View.GONE);
        }
        if (swipyRefreshLayout.isRefreshing()) {
            swipyRefreshLayout.setRefreshing(false);
        }

    }, error -> {

    });

    RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
    requestQueue.add(request);

}

有什么可以加速的吗?
我将实现sqlite来存储数据并在应用程序启动时显示。因此,用户感觉不到加载时间。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题