Android get play store app version

时光毁灭记忆、已成空白 提交于 2019-12-09 13:48:15

问题


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:


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

Update: 12/09/2019

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

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

        private String newVersion;

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



回答3:


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.




回答4:


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);
        }
    }



回答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();


来源:https://stackoverflow.com/questions/48926962/android-get-play-store-app-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!