Android 获取 Google Play 商店应用版本
Android get Google Play Store app version
我正在使用此代码获取 Google Play 商店应用程序版本,但这导致我的应用程序挂起。请指定另一种获取应用程序版本的方法,或者我如何使用此代码使应用程序不挂起并 运行 成功。
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;
}
}
这是我从 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();
}
改变这两行
{
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")
....
您正在使用 BuildConfig.APPLICATION_ID
,但您需要 package_name
BuildConfig
由 Gradle 提供。如果您不使用 Gradle
进行构建,则无法使用 BuildConfig
.
访问包名称
我会使用 Context.getPackageName()
因为结果是由操作系统提供的,而不是构建参数中的常量。
需要放入AsyncTask
//********* 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);
}
}
只需替换您的代码
.get()
.select("div[itemprop=softwareVersion]")
.first()
.ownText();
以下:
.get()
.select(".hAyfc .htlgb")
.get(3)
.ownText();
它会工作..!
出现问题是因为元素div [itemprop = softwareVersion]
在Google Play网站上找不到了(因此它不再是returns版本号),只能改成google 在您的网站上所做的更改的新查询。
注意:考虑到如果再次更改结构,则必须更改此代码。
希望对你有帮助
更新:2019 年 9 月 12 日
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;
}
}
科迪戈 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();
}
Google 将 API 从 'softwareVersion' 更改为 'currentVersion' 我们可以使用“.hAyfc .htlgb”获得相同的版本
下面的代码将不起作用,即
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();
除了上面的代码,只需添加这段代码,它就可以正常工作,即
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()].
试试这个,
替换代码,
.get()
.select("div:containsOwn(Current Version)")
.next()
.text();
您可以使用 UpdateManager 更轻松地实现此目的 - https://developer.android.com/reference/com/google/android/things/update/UpdateManager
您可以这样设置更新管理器
// Creates instance of the manager.
AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(this);
// Returns an intent object that you use to check for an update.
Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
//On Success Listener
appUpdateInfoTask.addOnSuccessListener(this);
在您的 On Success 方法中,您可以调用
result.availableVersionCode()
其中returns0时版本不一样。如果不同,您可以直接从您的应用程序中提取此值。
您似乎使用了 Google Play 商店网站上显示的“app-version”来向您的用户触发“更新弹出窗口”并在新版本发布时发出通知可用的。首先,Google Play 商店网站经常更改,因此这不是很可靠,因为您的应用可能无法“parse/handle”这些更改。如果您想保留这种方法,其他一些答案已经解释了如何解析正确的 HTML 标记以及如何在后台正确执行。请记住,您的应用程序仍将负责“解析”,最终您将在 web-server 上进行远程解析,并向您的应用程序公开一个稳定的 API 端点以检索“latest-version" 你自己的应用程序。如果您不想费心解析 HTML 标签或托管自己的 API,third-party API 可以为您完成。我们在这里提供了这样一个 API:https://42matters.com/docs/app-market-data/android/apps/lookup(它 returns 一个 JSON 具有应用程序最新“版本名称”的对象)。
您也可以使用 https://github.com/danielemaddaluno/Android-Update-Checker 来不实现您自己的代码,在幕后它或多或少与您所做的相同。
就我个人而言,我会使用像 Firebase Remote Config 这样的远程服务 https://firebase.google.com/docs/remote-config or a simple JSON file hosted on your website specifying the latest published version of your app (just remember to change it once the update of your app is really published on Google Play, nowadays it might take a while). Also, I would rather use the "versionCode" as explained here: https://developer.android.com/studio/publish/versioning
使用这种方法,您可以在需要时“触发”更新,更多控制在您身边(特别是在您想要恢复“请更新”消息的情况下,如果您在发布的最新版本中发现错误时很有用)
我正在使用此代码获取 Google Play 商店应用程序版本,但这导致我的应用程序挂起。请指定另一种获取应用程序版本的方法,或者我如何使用此代码使应用程序不挂起并 运行 成功。
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;
}
}
这是我从 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();
}
改变这两行
{
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")
....
您正在使用 BuildConfig.APPLICATION_ID
,但您需要 package_name
BuildConfig
由 Gradle 提供。如果您不使用 Gradle
进行构建,则无法使用 BuildConfig
.
我会使用 Context.getPackageName()
因为结果是由操作系统提供的,而不是构建参数中的常量。
需要放入AsyncTask
//********* 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);
}
}
只需替换您的代码
.get()
.select("div[itemprop=softwareVersion]")
.first()
.ownText();
以下:
.get()
.select(".hAyfc .htlgb")
.get(3)
.ownText();
它会工作..!
出现问题是因为元素div [itemprop = softwareVersion]
在Google Play网站上找不到了(因此它不再是returns版本号),只能改成google 在您的网站上所做的更改的新查询。
注意:考虑到如果再次更改结构,则必须更改此代码。
希望对你有帮助
更新:2019 年 9 月 12 日
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;
}
}
科迪戈 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();
}
Google 将 API 从 'softwareVersion' 更改为 'currentVersion' 我们可以使用“.hAyfc .htlgb”获得相同的版本
下面的代码将不起作用,即
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();
除了上面的代码,只需添加这段代码,它就可以正常工作,即
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()].
试试这个,
替换代码,
.get()
.select("div:containsOwn(Current Version)")
.next()
.text();
您可以使用 UpdateManager 更轻松地实现此目的 - https://developer.android.com/reference/com/google/android/things/update/UpdateManager
您可以这样设置更新管理器
// Creates instance of the manager.
AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(this);
// Returns an intent object that you use to check for an update.
Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
//On Success Listener
appUpdateInfoTask.addOnSuccessListener(this);
在您的 On Success 方法中,您可以调用
result.availableVersionCode()
其中returns0时版本不一样。如果不同,您可以直接从您的应用程序中提取此值。
您似乎使用了 Google Play 商店网站上显示的“app-version”来向您的用户触发“更新弹出窗口”并在新版本发布时发出通知可用的。首先,Google Play 商店网站经常更改,因此这不是很可靠,因为您的应用可能无法“parse/handle”这些更改。如果您想保留这种方法,其他一些答案已经解释了如何解析正确的 HTML 标记以及如何在后台正确执行。请记住,您的应用程序仍将负责“解析”,最终您将在 web-server 上进行远程解析,并向您的应用程序公开一个稳定的 API 端点以检索“latest-version" 你自己的应用程序。如果您不想费心解析 HTML 标签或托管自己的 API,third-party API 可以为您完成。我们在这里提供了这样一个 API:https://42matters.com/docs/app-market-data/android/apps/lookup(它 returns 一个 JSON 具有应用程序最新“版本名称”的对象)。
您也可以使用 https://github.com/danielemaddaluno/Android-Update-Checker 来不实现您自己的代码,在幕后它或多或少与您所做的相同。
就我个人而言,我会使用像 Firebase Remote Config 这样的远程服务 https://firebase.google.com/docs/remote-config or a simple JSON file hosted on your website specifying the latest published version of your app (just remember to change it once the update of your app is really published on Google Play, nowadays it might take a while). Also, I would rather use the "versionCode" as explained here: https://developer.android.com/studio/publish/versioning 使用这种方法,您可以在需要时“触发”更新,更多控制在您身边(特别是在您想要恢复“请更新”消息的情况下,如果您在发布的最新版本中发现错误时很有用)