0

I'm trying to fetch Report coronavirus cases table data from https://www.worldometers.info/coronavirus/ to my Android app using Java.

Report coronavirus cases

So I have an object called country and instance of the object are countryName, TotalCase, Total death, etc. and then I will store the object in an arrayList.

Here is my code:

import android.os.AsyncTask;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class SammaryHandler extends AsyncTask<Void,Void,Void> {
    @Override
    protected Void doInBackground(Void... voids) {
        try{
            Document document= Jsoup.connect("https://www.worldometers.info/coronavirus/").userAgent("mozilla/17.0").get();
//            Elements temp= document.select("table table-bordered table-hover main_table_countries dataTable no-footer");

            Elements temp= document.select("table.dataTable");//?????????????????

            int size= temp.size();
            for(int i=0; i<size;i++){
                String data= temp.select("table.dataTable").eq(i).attr("");//?????????????
                System.out.println(data);
            }
        }catch (Exception e){
            System.out.println(e);
        }
        return null;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }
    @Override
    protected void onCancelled() {
        super.onCancelled();
    }
}

So how can I read all of the data from the table and save load in the object?

1 Answer 1

1

Yeah just as an information for you from their website - it is not permitted to use their data without buying a license.

But answering your question. I see mainly that you haven't applied proper CSSQuery. Try this code:

      //connect to the website
final String url =
                "https://www.worldometers.info/coronavirus/";
//surround by try catch
        try {
            final Document document = Jsoup.connect(url).get();
    //create a list of what you need from the table. we will read data by each column
            List<String> countrieNames = null;
//loop by elements in the table
        for (Element row : document.select("table#main_table_countries_today tr")) {
//filter as there are empty rows or others to be filtered
                        if (row.select("td:nth-of-type(1)").text().equals("") || row.select("td:nth-of-type(1)").text().equals("Total:")) {
                              continue;
                            }
//save informations you need to the created list
                            countrieNames = document.select("td:nth-of-type(1)").eachText();

        }
//print out results
for (int i = 0; i < countrieNames.size(); i=i+2) {
                    System.out.println(countrieNames.get(i));
                }
} catch(Exception e){
                e.printStackTrace();
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.