0

I am using below code to get versionName from Google play store by using Jsoup for fetching details but its throwing some exception, But recently it was working fine. I don't know what happen now?

My code is -

public class VersionChecker extends AsyncTask<String, String, String> {

    private String newVersion;

    @Override
    protected String doInBackground(String... params) {

        try {
            newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + ParkwayApplication.getInstance().getPackageName() + "&hl=en")
                    .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()
                    .select("div[itemprop=softwareVersion]")
                    .first()
                    .ownText();
            Log.d("otherhtml", newVersion);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return newVersion;
    }
}

Note - In above code the select() returns size 0 and when i call first() it will return null and that's why getting null pointer exception.

Note - I don't have any idea why it was working recently why not working now?

Here is the link I tried and this but no solution.

This library version I am using now -

compile 'org.jsoup:jsoup:1.8.3'

But I tried latest one too -

compile 'org.jsoup:jsoup:1.11.3'

Below is exact error I am getting -

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 
'java.lang.String org.jsoup.nodes.Element.ownText()' on a null object reference 
   at com.ranosys.parkway.utils.VersionChecker.doInBackground(VersionChecker.java:31)
   at com.ranosys.parkway.utils.VersionChecker.doInBackground(VersionChecker.java:16)
   at android.os.AsyncTask$2.call(AsyncTask.java:295)
   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
   at java.lang.Thread.run(Thread.java:818) 
Bajrang Hudda
  • 3,028
  • 1
  • 36
  • 63
  • Without exception error from logs we can't help you. However I remember I had similar problems with this and my problem was related to threads. – zemaitis Apr 23 '18 at 09:33
  • Did you tried to do something obvious like check if source of this webpage contains any "softwareVersion" text at all? *I don't have any idea why it was working recently why not working now?* did you think about something obvious like: **google changed their website ...** – Selvin Apr 23 '18 at 09:37
  • @Selvin I don't have any Idea if Google change or not, I am just trying to get play store app version, Actaully this was the way of getting the app version that is many developers using. – Bajrang Hudda Apr 23 '18 at 09:41
  • This code is parsing some 3rd party website and search for particullar div which may not exists ... and if it doesn't exist you can't do anything about this – Selvin Apr 23 '18 at 09:45
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Manoj Perumarath Apr 23 '18 at 09:45
  • well this is not duplicate of this question as he is know why he is NPE **please read the question:** *In above code the select() returns size 0 and when i call first() it will return null and that's why getting null pointer exception* – Selvin Apr 23 '18 at 09:46
  • @Selvin, I agree with you, What code should write here that can work me? – Bajrang Hudda Apr 23 '18 at 09:46
  • own some backend and store version there ... – Selvin Apr 23 '18 at 09:48
  • @Selvin, Yeah, I am also perfer this but this work is done by any other person, and now we got issue, so trying to resolve in same way. – Bajrang Hudda Apr 23 '18 at 09:49

2 Answers2

6

I often parse information from Google. In my opinion, don't choose generated class names like .xyOfqd, as they often change. I prefer to search for constant texts via regex.

Element element = document.select("div:matchesOwn(^Current Version$)").first().parent().select("span").first();
String version = element.text();

You get the nullpointer exeption because the page was changed. I have reviewed several apps in the Playstore. None had your div named [itemprop = softwareversion] anymore. So you're calling .first() on null because your select() will not return any results.

Just if you need an another aproach =)

3

Try with this:

public class VersionChecker extends AsyncTask<String, Document, Document> {

        private Document document;

        @Override
        protected Document doInBackground(String... params) {

            try {
                document = Jsoup.connect("https://play.google.com/store/apps/details?id="+ParkwayApplication.getInstance().getPackageName() +"&hl=en")
                        .timeout(30000)
                        .userAgent("Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")
                        .get();

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

            return document;
        }

        @Override
        protected void onPostExecute(Document d) {
            super.onPostExecute(d);

            Elements es =  d.body().getElementsByClass("xyOfqd").select(".hAyfc");
            String newVersion = es.get(3).child(1).child(0).child(0).ownText();
            Log.i(TAG, "newVersion==="+newVersion);
        }
    }
Deven
  • 3,078
  • 1
  • 32
  • 34
  • some near future(Musk still didn't send ship on Mars) Bajrang Hudda is asking another question on stackoverflow: *I'm using this code ... `code from this answer here` ... it is not working ... But recently it was working fine* – Selvin Apr 25 '18 at 22:05
  • Also it is useless fx for `com.google.android.gms` it returns *Varies with device* – Selvin Apr 25 '18 at 22:13
  • Actually the child has changed since the answer, now the info is in the child val es =doc.body()?.getElementsByClass("xyOfqd")?.select(".hAyfc")?.select(".htlgb") newVersion = es.get(4).child(0).child(0).ownText() – Jonathan Jun 29 '18 at 14:25