0

I'm trying to web scrape from a website https://www.worldometers.info/coronavirus/ and turns that data to form an app but the data is not actually printing I don't the reason but whenever I click the button in an android emulator it just crashes instantly !!

I have 3 textView in the app and a button so whenever I click the button it should show up the data in the textView !!

textView have ids of TotalCases / TotalDeaths / TotalRecovered button has an id of the button

Here is what I have I have done

package com.example.coronaupdate;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.widget.Button;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Button btn;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                compare();
            }
        });

    }
    public void compare()
    {
        final TextView totalCases;
        final  TextView totalDeaths;
        final TextView totalRecovered;

        totalCases = (TextView) findViewById(R.id.TotalCases);
        totalDeaths = (TextView) findViewById(R.id.TotalDeaths);
        totalRecovered = (TextView) findViewById(R.id.TotalRecovered);

        try {
            Document doc = Jsoup.connect("https://www.worldometers.info/coronavirus/").userAgent("mozilla/17.0").get();
            Elements temp = doc.select("div.maincounter-number");


            Element totalcase = temp.get(0);
            String cases = totalcase.select("div.maincounter-number span").text();
            totalCases.setText(cases);

            Element totaldeaths = temp.get(1);
            String deaths = totaldeaths.select("div.maincounter-number span").text();
            totalDeaths.setText(deaths);

            Element totalrecovered = temp.get(2);
            String recovered = totalrecovered.select("div.maincounter-number span").text();
            totalRecovered.setText(recovered);

           /* for(Element totalCase:temp)
            {
                String cases = totalCase.select("div.maincounter-number span").text();
                System.out.println("" + cases);
                *//*i++;
                System.out.println(i + "" + totalCase.getElementsByTag("span"));*//*
            }*/
        }

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

image of the app view

Image of the app view

1 Answers1

0

If you could post the Error you're getting it would be easier to see what's going on, but I have two assumptions of what's wrong.

First you should check if yout Manifest is asking for INTERNET access permission. If it isn't, you should include it.

Second: Always when making requests to the internet in Android you should use either AsyncTasks or at least open a separate thread manually. That's because internet calls are asynchronous by definition, and if you block the main execution of your App to wait for a response the UI is gonna freeze or crash. So perhaps you should extract the logic of your compare() function to a separate class that inherits from AsyncTask and place it into the doInBackground() method.

J. Maruca
  • 48
  • 6