S.Vishwakarma
S.Vishwakarma

Reputation: 129

Get current app version on play store

I had implemented the code to get the current app version of my app on play store, so that i can suggest user to upgrade app whenever any new version is there on play store. This code was working fine, but now it is not working.

The code to get current app version that I have used is as follows:

newVersion = Jsoup.connect(
    "https://play.google.com/store/apps/details?id=" +
    "<app package name>" + "&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();

It seems goole play store web page has now got updated and i am unable to find any div with itemprops=softwareVersion. I have checked the source code of google play store webpage of my app but it does not show the current app version there also.

The exception i am getting is:

Process: com.srsvalutech.theparkking, PID: 25357
java.lang.RuntimeException: An error occured while executing doInBackground()
 at android.os.AsyncTask$3.done(AsyncTask.java:300)
 at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
 at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
 at java.util.concurrent.FutureTask.run(FutureTask.java:242)
 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
 at java.lang.Thread.run(Thread.java:818)
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.srsvalutech.theparkking.view.SplashScreenActivity$GetAppCurrentVersionAsyncTask.doInBackground(SplashScreenActivity.java:124)
 at com.srsvalutech.theparkking.view.SplashScreenActivity$GetAppCurrentVersionAsyncTask.doInBackground(SplashScreenActivity.java:102)
 at android.os.AsyncTask$2.call(AsyncTask.java:288)
 at java.util.concurrent.FutureTask.run(FutureTask.java:237)
 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
 at java.lang.Thread.run(Thread.java:818) 

Is there any other way by which we can get the current app version of an android app on play store programmatically in android.

Upvotes: 3

Views: 2091

Answers (3)

Gast&#243;n Saill&#233;n
Gast&#243;n Saill&#233;n

Reputation: 13159

This library is based on JSoup to parse HTML: https://github.com/danielemaddaluno/Android-Update-Checker

A basic implementation of what you want will be like this:

private boolean web_update(){
    try {       
        String curVersion = applicationContext.getPackageManager().getPackageInfo(package_name, 0).versionName; 
        String newVersion = curVersion;
        newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + package_name + "&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();
        return (value(curVersion) < value(newVersion)) ? true : false;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

If you want only to verify a mismatch between versions, you can change:

value(curVersion) < value(newVersion) with value(curVersion) != value(newVersion)

Upvotes: 0

R Thompson
R Thompson

Reputation: 11

Solution that worked for us!

.select("div:containsOwn(Current Version) + div")
                                          .first()
                                          .text(); 

Upvotes: 1

Chris
Chris

Reputation: 147

I had the same problem and the answer of jppavez from this question: How to get the play store version number for Xamarin Android worked for me.

Just replace

.select("div[itemprop=softwareVersion]")
.first()
.ownText();

with

.select("div:containsOwn(Current Version)")
.next()
.text();

EDIT: Don't forget to catch the RuntimeException to prevent your application from crashing if google does changes on the source of the Play Store again. For example combined with a crashlytics crashreport:

    } catch (RuntimeException e) {
        Crashlytics.logException(e);

Upvotes: 2

Related Questions