Search code examples
androidandroid-studiojsoup

How to use Jsoup in an Android app to show certain text from a website?


I want to learn jsoup and went through some tutorials and the documentation, but they are outdated and the functions used son't work anymore. I understood how we get a website into a document by

Document document = (Document) Jsoup.connect("https://crackwatch.com/").get();

But what after it?

For example I want to get the text Cracked or Uncracked from this page: https://crackwatch.com/game/detroit-become-human

How do i do that? Please give the code along with what each line does.


Solution

  • I am using JSOUP to get "Current Version" from PlayStore to ForceUpdate the app, you can take help from my code :

     private class ForceUpdateAsync extends AsyncTask<Void, String, String> {
    
            private Context context;
            private String currentVersion;
            private AppStartupThreadResponse response;
    
            public ForceUpdateAsync(Context context, String currentVersion, AppStartupThreadResponse response) {
                this.context = context;
                this.currentVersion = currentVersion;
                this.response = response;
            }
    
            @Override
            protected String doInBackground(Void... voids) {
                String newVersion = null;
                try {
                    //HTML Parsing of the data coming from the url
                    Document document = Jsoup.connect("https://play.google.com/store/apps/details?id=com.vassar.unifiedapp.dmaedu&hl=en_IN")
                            .timeout(30000)
                            .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"))
                            .referrer("http://www.google.com")
                            .get();
                    if (document != null) {
    
                        Elements element = document.getElementsContainingOwnText("Current Version");
                        for (Element ele : element) {
                            if (ele.siblingElements() != null) {
                                Elements sibElemets = ele.siblingElements();
                                for (Element sibElemet : sibElemets) {
                                    newVersion = sibElemet.text();
                                }
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return newVersion;
            }