Android get play store app version

2019-04-14 17:20发布

问题:

I am using this code to get play store app version but this is causing my app to hang. Please specify other way to get play store app version or how i can use this code to make app not hang and run successfully. Thanks in advance.

    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=" + "trai.gov.in.dnd" + "&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();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newVersion;
    }
}

This is my code to get version from asynctask

VersionChecker versionChecker = new VersionChecker(); 
try 
{ String appVersionName = BuildConfig.VERSION_NAME; 
String mLatestVersionName = versionChecker.execute().get(); 
if (versionChecker.compareVersionNames(appVersionName, mLatestVersionName) == -1) 
{ 
showAppUpdateDialog(); 
} 
} catch (InterruptedException | ExecutionException e) { 
e.printStackTrace(); 
} 

回答1:

Just replace your code

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

with below:

                   .get()
                   .select(".hAyfc .htlgb")
                   .get(3)
                   .ownText();

it will work..!



回答2:

Change this two lines

{
 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")
....

you are using BuildConfig.APPLICATION_ID but you need package_name

BuildConfig is provided by Gradle. If you are not building using Gradle then you cannot access the package name using BuildConfig.

I would use Context.getPackageName() because the result is provided from the operating system, rather than a constant in build parameters.



回答3:

You need to put it into AysncTask

 //********* Check for Updated Version of APP
    private class GetVersionCode extends AsyncTask<Void, String, String> {
        String currentVersion = null;

        @Override
        protected String doInBackground(Void... voids) {

            String newVersion = null;

            try {
                currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
                newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + Activity.this.getPackageName() + "&hl=it")
                        .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 newVersion;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String onlineVersion) {
            super.onPostExecute(onlineVersion);
            if (onlineVersion != null && !onlineVersion.isEmpty()) {
                if (!currentVersion.equalsIgnoreCase(onlineVersion)) {
                    //Todo code here

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


回答4:

The problem arises because the element div [itemprop = softwareVersion] is no longer found on the Google Play website (therefore it no longer returns the version number), it should only be changed to a new query with the changes made by google on your website.

Note: Take into account that if the structure is changed again, this code must be changed.

I hope it helps

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=" + "trai.gov.in.dnd" + "&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(".xyOfqd .hAyfc:nth-child(4) .htlgb span")
                    .get(0)
                    .ownText();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newVersion;
    }
}

Codigo Java

VersionChecker versionChecker = new VersionChecker();
try {
    String latestVersion = versionChecker.execute().get();
    String versionName = BuildConfig.VERSION_NAME.replace("-DEBUG","");
    if (latestVersion != null && !latestVersion.isEmpty()) {
        if (!latestVersion.equals(versionName)) {
            showDialogToSendToPlayStore();
        }else{
            continueWithTheLogin();
        }
    }
    Log.d("update", "Current version " + Float.valueOf(versionName) + ", Playstore version " + Float.valueOf(latestVersion));

} catch (InterruptedException e) {
    e.printStackTrace();
} catch (ExecutionException e) {
    e.printStackTrace();
}


回答5:

Google change the API from 'softwareVersion' to 'currentVersion' and we can get the same version using ".hAyfc .htlgb"

Below code will not work i.e.

latestVersion = 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().select("div[itemprop=softwareVersion]").first().ownText();

Instead of above code just add this code, it will work fine i.e.

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) {
        if(document.select(".hAyfc .htlgb").get(5) != null) {
            latestVersion = document.select(".hAyfc .htlgb").get(5).ownText();
        } else {
            latestVersion = currentVersion;
        }
    }

Note: There is no official API to get the current application version from Google Play. So use the above code to get the current version. Sometimes get(5).ownText() will change as google is updating the Google Play site. [Previously it was working with get(2).ownText()].


回答6:

just try this,

Replace the code ,

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