0

I got the application of version 1.1.2 in the playstore. I am checking the update of the playstore in the application but I got the error. I researched but couldnot get any solution. I got stuck with the error NumberFormatException.I have implemented as follows:

 try {
        currentVersion = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

I have tried to fetch the version number of the application from playstore.

private class GetVersionCode extends AsyncTask<Void, String, String> {

    @Override

    protected String doInBackground(Void... voids) {

        String newVersion = null;

        try {
            Document document = Jsoup.connect("https://play.google.com/store/apps/details?id="+context.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();
            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;

    }


    @Override

    protected void onPostExecute(String onlineVersion) {

        super.onPostExecute(onlineVersion);

        if (onlineVersion != null && !onlineVersion.isEmpty()) {

            if (Float.valueOf(currentVersion) < Float.valueOf(onlineVersion)) {
                //show anything
            }

        }

        Log.d("update", "Current version " + currentVersion + "playstore version " + onlineVersion);

    }
}
Nabin Dhakal
  • 1,949
  • 3
  • 20
  • 48

2 Answers2

1

You need to use..

.versionCode // this will return integer

.versionName will return string version name. while versionCode will return integer.

But as per your code and functionality... you have to use the version name in floating like String(eg:- 1.1 or 1.2 etc). then you can use version name also.

SRB Bans
  • 3,096
  • 1
  • 10
  • 21
1

currentVersion and onlineVersion both are of type String. 1.1.2 is a String and not a number.

Instead of comparing them as Floats (which doesn't work as they are Strings):

if (Float.valueOf(currentVersion) < Float.valueOf(onlineVersion)) {
   //show anything
}

You should compare them as Strings to check if the versions are different:

if (!onlineVersion.equals(currentVersion)) {
   //show anything
}

If you want to compare if the onlineVerson is newer (instead of just different) you should take a look at that solution: How do you compare two version Strings in Java?

The preferred way of comparing Android app versions would of course be through the versionCode because it is of type int and needs to increase with every new version. versionName can be an arbitrary value and nothing keeps you from naming the first version 1.0 and the following versions 0.8 or Mountain Lion, making them hard to compare.

A better solution might be to store the most recent versionCode on a webserver and check for this code instead of going to the Play Store and parsing the store listing.

Markus Penguin
  • 1,581
  • 12
  • 19