如何使用JSoup进行分页

时间:2018-06-21 14:15:54

标签: java android jsoup

我正在与JSoup合作,并且能够在RecyclerView中列出网站的数据。但我想在其中放一个页面。我按照下面的教程进行操作,但是只显示了前几页。有人知道我该怎么做吗?

教程:http://www.yudiz.com/pagination-data-scraping-in-android-using-jsoupjava-html-parser/

private class Description extends AsyncTask<Void, Void, Void> {
       String desc;
 
       @Override
       protected void onPreExecute() {
           super.onPreExecute();
           mProgressDialog = new ProgressDialog(MainActivity.this);
           mProgressDialog.setTitle("Android Basic JSoup Tutorial");
           mProgressDialog.setMessage("Loading...");
           mProgressDialog.setIndeterminate(false);
           mProgressDialog.show();
       }
 
       @Override
       protected Void doInBackground(Void... params) {
           try {
               // Connect to the web site
               Document mBlogDocument = Jsoup.connect(url).get();
               // Using Elements to get the Meta data
               Elements mElementDataSize = mBlogDocument.select("div[class=author-date]");
               // Locate the content attribute
               int mElementSize = mElementDataSize.size();
 
               for (int i = 0; i < mElementSize; i++) {
                   Elements mElementAuthorName = mBlogDocument.select("span[class=vcard author post-author test]").select("a").eq(i);
                   String mAuthorName = mElementAuthorName.text();
 
                   Elements mElementBlogUploadDate = mBlogDocument.select("span[class=post-date updated]").eq(i);
                   String mBlogUploadDate = mElementBlogUploadDate.text();
 
                   Elements mElementBlogTitle = mBlogDocument.select("h2[class=entry-title]").select("a").eq(i);
                   String mBlogTitle = mElementBlogTitle.text();
 
                   mAuthorNameList.add(mAuthorName);
                   mBlogUploadDateList.add(mBlogUploadDate);
                   mBlogTitleList.add(mBlogTitle);
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
           return null;
       }
 
       @Override
       protected void onPostExecute(Void result) {
           // Set description into TextView
 
           RecyclerView mRecyclerView = (RecyclerView)findViewById(R.id.act_recyclerview);
 
           DataAdapter mDataAdapter = new DataAdapter(MainActivity.this, mBlogTitleList, mAuthorNameList, mBlogUploadDateList);
           RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
           mRecyclerView.setLayoutManager(mLayoutManager);
           mRecyclerView.setAdapter(mDataAdapter);
 
           mProgressDialog.dismiss();
       }
   }

1 个答案:

答案 0 :(得分:0)

尝试

List<String> mBlogTitleList = new ArrayList();  
List<String> mAuthorNameList = new ArrayList();
List<String> mBlogUploadDateList = new ArrayList();

RecyclerView mRecyclerView = (RecyclerView)findViewById(R.id.act_recyclerview);
DataAdapter mDataAdapter = new DataAdapter(MainActivity.this, mBlogTitleList, mAuthorNameList, mBlogUploadDateList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mDataAdapter);

private class Description extends AsyncTask<Void, Void, Void> {
       String desc;
       List<String> mBlogTitleListDescription = new ArrayList();    
       List<String> mAuthorNameListDescription = new ArrayList();   
       List<String> mBlogUploadDateListDescription = new ArrayList();   
       @Override
       protected void onPreExecute() {
           super.onPreExecute();
           mProgressDialog = new ProgressDialog(MainActivity.this);
           mProgressDialog.setTitle("Android Basic JSoup Tutorial");
           mProgressDialog.setMessage("Loading...");
           mProgressDialog.setIndeterminate(false);
           mProgressDialog.show();
       }

       @Override
       protected Void doInBackground(Void... params) {
           try {
               // Connect to the web site
               Document mBlogDocument = Jsoup.connect(url).get();
               // Using Elements to get the Meta data
               Elements mElementDataSize = mBlogDocument.select("div[class=author-date]");
               // Locate the content attribute
               int mElementSize = mElementDataSize.size();

               for (int i = 0; i < mElementSize; i++) {
                   Elements mElementAuthorName = mBlogDocument.select("span[class=vcard author post-author test]").select("a").eq(i);
                   String mAuthorName = mElementAuthorName.text();

                   Elements mElementBlogUploadDate = mBlogDocument.select("span[class=post-date updated]").eq(i);
                   String mBlogUploadDate = mElementBlogUploadDate.text();

                   Elements mElementBlogTitle = mBlogDocument.select("h2[class=entry-title]").select("a").eq(i);
                   String mBlogTitle = mElementBlogTitle.text();

                   mAuthorNameListDescription.add(mAuthorName);
                   mBlogUploadDateListDescription.add(mBlogUploadDate);
                   mBlogTitleListDescription.add(mBlogTitle);
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
           return null;
       }

       @Override
       protected void onPostExecute(Void result) {
           // Set description into TextView
          mBlogTitleList.addAll(mBlogTitleListDescription);
          mAuthorNameList.addAll(mAuthorNameListDescription);
          mBlogUploadDateList.addAll(mBlogUploadDateListDescription);
          mDataAdapter.notifyDataSetChanged();

           mProgressDialog.dismiss();
       }
   }