如何从Android中的JSON加载更多页面数据?

时间:2016-04-23 07:47:18

标签: android json

我想开发一个网站的Android应用程序!在这个网站上,任何页面显示10个帖子,其他帖子转到页面。我可以只显示10个帖子(第1页),并在向下滚动时显示 ,在以下页面上显示更多帖子将显示10个条目
对于展示帖子,我使用RecyclerView组件。

Json(来自网站):

{
    "status": "ok",
    "count": 10,
    "count_total": 13,
    "pages": 2,
    "posts": [{
        "id": 88,
        "type": "post",
        "slug": "%d8%b1%d9%81%d8%b1%d8%b4-%da%a9%d9%86",
        "url": "http:\/\/tellfa.com\/tafrihgah\/?p=88",
        "status": "publish",
        "title": "\u0631\u0641\u0631\u0634 \u06a9\u0646",
        "title_plain": "\u0631\u0641\u0631\u0634 \u06a9\u0646",
        "content": "<p>\u0644\u0627\u0645\u0635\u0628 \u0631\u0641\u0631\u0634 \u06a9\u0646 \u062f\u06cc\u06af\u0647\u060c \u0686\u062a\u0647 \u0622\u062e\u0647 <img src=\"http:\/\/tellfa.com\/tafrihgah\/wp-includes\/images\/smilies\/frownie.png\" alt=\":(\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n",
        "excerpt": "<p>\u0644\u0627\u0645\u0635\u0628 \u0631\u0641\u0631\u0634 \u06a9\u0646 \u062f\u06cc\u06af\u0647\u060c \u0686\u062a\u0647 \u0622\u062e\u0647 <\/p>\n",
        "date": "2016-04-20 15:31:55",
        "modified": "2016-04-20 15:31:55",
        "categories": [{
            "id": 1,
            "slug": "%d8%af%d8%b3%d8%aa%d9%87%e2%80%8c%d8%a8%d9%86%d8%af%db%8c-%d9%86%d8%b4%d8%af%d9%87",
            "title": "\u062f\u0633\u062a\u0647\u200c\u0628\u0646\u062f\u06cc \u0646\u0634\u062f\u0647",
            "description": "",
            "parent": 0,
            "post_count": 10
        }],
        "tags": [],
        "author": {
            "id": 1,
            "slug": "tellfa",
            "name": "\u0645\u062d\u0645\u062f",
            "first_name": "",
            "last_name": "",
            "nickname": "\u0645\u062d\u0645\u062f",
            "url": "http:\/\/codesaz.com",
            "description": "\u0627\u06cc\u0646 \u0632\u0646\u062f\u06af\u06cc \u0646\u0627\u0645\u0647 \u0645\u0646 \u0627\u0633\u062a",
            "avatar": "76"
        },
        "comments": [],
        "attachments": [],
        "comment_count": 0,
        "comment_status": "open",
        "thumbnail": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/Beautiful-Mix-HD-Wallpaper-06-p30download.com-064-150x150.jpg",
        "custom_fields": {},
        "thumbnail_size": "thumbnail",
        "thumbnail_images": {
            "full": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/Beautiful-Mix-HD-Wallpaper-06-p30download.com-064.jpg",
                "width": 1920,
                "height": 1200
            },
            "thumbnail": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/Beautiful-Mix-HD-Wallpaper-06-p30download.com-064-150x150.jpg",
                "width": 150,
                "height": 150
            },
            "medium": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/Beautiful-Mix-HD-Wallpaper-06-p30download.com-064-300x188.jpg",
                "width": 300,
                "height": 188
            }
        }
    }, {
        "id": 85,
        "type": "post",
        "slug": "%d8%b9%d9%86%d9%88%d8%a7%d9%86-%d8%b3%d9%88%d9%85-%d8%a8%d8%b1%d8%a7%db%8c-%d8%b1%d9%81%d8%b1%d8%b4",
        "url": "http:\/\/tellfa.com\/tafrihgah\/?p=85",
        "status": "publish",
        "title": "\u0639\u0646\u0648\u0627\u0646 \u0633\u0648\u0645 \u0628\u0631\u0627\u06cc \u0631\u0641\u0631\u0634",
        "title_plain": "\u0639\u0646\u0648\u0627\u0646 \u0633\u0648\u0645 \u0628\u0631\u0627\u06cc \u0631\u0641\u0631\u0634",
        "content": "<p>\u062f\u06cc\u06af\u0647 \u0639\u0635\u0628\u0627\u0646\u06cc \u0634\u062f\u0645\u060c \u0686\u0631\u0627 \u0631\u0641\u0631\u0634 \u0646\u0645\u06cc\u06a9\u0646\u0647! :@<\/p>\n",
        "excerpt": "<p>\u062f\u06cc\u06af\u0647 \u0639\u0635\u0628\u0627\u0646\u06cc \u0634\u062f\u0645\u060c \u0686\u0631\u0627 \u0631\u0641\u0631\u0634 \u0646\u0645\u06cc\u06a9\u0646\u0647! :@<\/p>\n",
        "date": "2016-04-20 15:02:26",
        "modified": "2016-04-20 15:02:26",
        "categories": [{
            "id": 1,
            "slug": "%d8%af%d8%b3%d8%aa%d9%87%e2%80%8c%d8%a8%d9%86%d8%af%db%8c-%d9%86%d8%b4%d8%af%d9%87",
            "title": "\u062f\u0633\u062a\u0647\u200c\u0628\u0646\u062f\u06cc \u0646\u0634\u062f\u0647",
            "description": "",
            "parent": 0,
            "post_count": 10
        }],
        "tags": [],
        "author": {
            "id": 1,
            "slug": "tellfa",
            "name": "\u0645\u062d\u0645\u062f",
            "first_name": "",
            "last_name": "",
            "nickname": "\u0645\u062d\u0645\u062f",
            "url": "http:\/\/codesaz.com",
            "description": "\u0627\u06cc\u0646 \u0632\u0646\u062f\u06af\u06cc \u0646\u0627\u0645\u0647 \u0645\u0646 \u0627\u0633\u062a",
            "avatar": "76"
        },
        "comments": [],
        "attachments": [{
            "id": 86,
            "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol119-024.jpg",
            "slug": "wallpapersmania_vol119-024",
            "title": "[WallpapersMania]_vol119-024",
            "description": "",
            "caption": "",
            "parent": 85,
            "mime_type": "image\/jpeg",
            "images": {
                "full": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol119-024.jpg",
                    "width": 1680,
                    "height": 1050
                },
                "thumbnail": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol119-024-150x150.jpg",
                    "width": 150,
                    "height": 150
                },
                "medium": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol119-024-300x188.jpg",
                    "width": 300,
                    "height": 188
                },
                "martial-frontpage-blog": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol119-024-771x376.jpg",
                    "width": 771,
                    "height": 376
                },
                "martial-blog-profile": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol119-024-371x267.jpg",
                    "width": 371,
                    "height": 267
                },
                "martial-blog-profile-thumb": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol119-024-74x67.jpg",
                    "width": 74,
                    "height": 67
                }
            }
        }],
        "comment_count": 0,
        "comment_status": "open",
        "thumbnail": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol119-024-150x150.jpg",
        "custom_fields": {},
        "thumbnail_size": "thumbnail",
        "thumbnail_images": {
            "full": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol119-024.jpg",
                "width": 1680,
                "height": 1050
            },
            "thumbnail": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol119-024-150x150.jpg",
                "width": 150,
                "height": 150
            },
            "medium": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol119-024-300x188.jpg",
                "width": 300,
                "height": 188
            },
            "martial-frontpage-blog": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol119-024-771x376.jpg",
                "width": 771,
                "height": 376
            },
            "martial-blog-profile": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol119-024-371x267.jpg",
                "width": 371,
                "height": 267
            },
            "martial-blog-profile-thumb": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol119-024-74x67.jpg",
                "width": 74,
                "height": 67
            }
        }
    }, {
        "id": 81,
        "type": "post",
        "slug": "%d8%b9%d9%86%d9%88%d8%a7%d9%86-%d8%af%d9%88%d9%85",
        "url": "http:\/\/tellfa.com\/tafrihgah\/?p=81",
        "status": "publish",
        "title": "\u0639\u0646\u0648\u0627\u0646 \u062f\u0648\u0645",
        "title_plain": "\u0639\u0646\u0648\u0627\u0646 \u062f\u0648\u0645",
        "content": "<p>\u0627\u06cc\u0646 \u0627\u062f\u0627\u0645\u0647 \u0645\u0637\u0644\u0628 \u0639\u0646\u0648\u0627\u0646 \u062f\u0648\u0645\u0647 \u062f\u06cc\u06af\u0647 \ud83d\ude00<\/p>\n",
        "excerpt": "<p>\u0627\u06cc\u0646 \u0627\u062f\u0627\u0645\u0647 \u0645\u0637\u0644\u0628 \u0639\u0646\u0648\u0627\u0646 \u062f\u0648\u0645\u0647 \u062f\u06cc\u06af\u0647 \ud83d\ude00<\/p>\n",
        "date": "2016-04-20 14:47:52",
        "modified": "2016-04-20 14:58:07",
        "categories": [{
            "id": 1,
            "slug": "%d8%af%d8%b3%d8%aa%d9%87%e2%80%8c%d8%a8%d9%86%d8%af%db%8c-%d9%86%d8%b4%d8%af%d9%87",
            "title": "\u062f\u0633\u062a\u0647\u200c\u0628\u0646\u062f\u06cc \u0646\u0634\u062f\u0647",
            "description": "",
            "parent": 0,
            "post_count": 10
        }],
        "tags": [],
        "author": {
            "id": 1,
            "slug": "tellfa",
            "name": "\u0645\u062d\u0645\u062f",
            "first_name": "",
            "last_name": "",
            "nickname": "\u0645\u062d\u0645\u062f",
            "url": "http:\/\/codesaz.com",
            "description": "\u0627\u06cc\u0646 \u0632\u0646\u062f\u06af\u06cc \u0646\u0627\u0645\u0647 \u0645\u0646 \u0627\u0633\u062a",
            "avatar": "76"
        },
        "comments": [],
        "attachments": [{
            "id": 82,
            "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol55-008.jpg",
            "slug": "wallpapersmania_vol55-008",
            "title": "[WallpapersMania]_vol55-008",
            "description": "",
            "caption": "",
            "parent": 81,
            "mime_type": "image\/jpeg",
            "images": {
                "full": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol55-008.jpg",
                    "width": 1280,
                    "height": 1024
                },
                "thumbnail": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol55-008-150x150.jpg",
                    "width": 150,
                    "height": 150
                },
                "medium": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol55-008-300x240.jpg",
                    "width": 300,
                    "height": 240
                },
                "martial-frontpage-blog": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol55-008-771x376.jpg",
                    "width": 771,
                    "height": 376
                },
                "martial-blog-profile": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol55-008-371x267.jpg",
                    "width": 371,
                    "height": 267
                },
                "martial-blog-profile-thumb": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol55-008-74x67.jpg",
                    "width": 74,
                    "height": 67
                }
            }
        }],
        "comment_count": 0,
        "comment_status": "open",
        "thumbnail": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol55-008-150x150.jpg",
        "custom_fields": {},
        "thumbnail_size": "thumbnail",
        "thumbnail_images": {
            "full": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol55-008.jpg",
                "width": 1280,
                "height": 1024
            },
            "thumbnail": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol55-008-150x150.jpg",
                "width": 150,
                "height": 150
            },
            "medium": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol55-008-300x240.jpg",
                "width": 300,
                "height": 240
            },
            "martial-frontpage-blog": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol55-008-771x376.jpg",
                "width": 771,
                "height": 376
            },
            "martial-blog-profile": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol55-008-371x267.jpg",
                "width": 371,
                "height": 267
            },
            "martial-blog-profile-thumb": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol55-008-74x67.jpg",
                "width": 74,
                "height": 67
            }
        }
    }, {

AsyncTask代码:

public class MainDataInfo {
    private Context mContext;

    public void getMainDataInfo(Context context) {
        mContext = context;
        new getInfo().execute(ServerIP.getIP());
    }

    public void getMainUpdateData(){
        new getInfo().execute();
    }

    private class getInfo extends AsyncTask<String, Void, String> {
        EventBus bus = EventBus.getDefault();
        private String ou_response;
        private List<MainDataModel> infoModels;

        @Override
        protected void onPreExecute() {
            //CustomProcessDialog.createAndShow(mContext);
            infoModels = new ArrayList<>();
        }

        @Override
        protected String doInBackground(String... params) {
            OkHttpClient client = new OkHttpClient();

            Request request = new Request.Builder()
                    .url(ServerIP.getIP())
                    .build();

            Response response;
            try {
                response = client.newCall(request).execute();
                ou_response = response.body().string();
                response.body().close();
                if (ou_response != null) {
                    try {
                        JSONObject postObj = new JSONObject(ou_response);
                        JSONArray postsArray = postObj.getJSONArray("posts");
                        infoModels = new ArrayList<>();

                        for (int i = 0; i < postsArray.length(); i++) {
                            JSONObject postObject = postsArray.getJSONObject(i);
                            int id = postObject.getInt("id");
                            String title = postObject.getString("title");
                            Log.d("Data", "Post id: " + id);
                            Log.d("Data", "Post title: " + title);

                            //Use the title and id as per your requirement
                            infoModels.add(new MainDataModel(
                                    postObject.getInt("id"),
                                    postObject.getString("title"),
                                    postObject.getString("content"),
                                    postObject.getString("thumbnail")));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return ou_response;
        }

        @Override
        protected void onPostExecute(String result) {
            //CustomProcessDialog.dissmis();
            if (result != null) {
                bus.post(infoModels);
            }
        }
    }
}

当向下滚动时,如何在10个帖子下方显示其他页面? 注意:我是业余开发者,不要给我负面评价。我真的需要你的教程和指南。谢谢所有&lt; 3

1 个答案:

答案 0 :(得分:0)

使用RecyclerView尝试这样的事情:

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
.
.
.
private boolean loading = true;
int pastVisiblesItems, visibleItemCount, totalItemCount;

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() 
{
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) 
    {
        if(dy > 0){

            visibleItemCount = layoutManager.getChildCount();
            totalItemCount = layoutManager.getItemCount();
            pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();

            if (loading) 
            {
                if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount) 
                {
                    loading = false;
                    //load new data
                }
            }
        }
    }
});