【发布时间】:2018-09-26 05:39:27
【问题描述】:
我在playstore里拿到了1.1.2版本的应用。我正在检查应用程序中 playstore 的更新,但出现错误。我进行了研究,但找不到任何解决方案。我遇到了 NumberFormatException 错误。我的实现如下:
try {
currentVersion = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
我已尝试从 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);
}
}
【问题讨论】:
-
你的
currentVersion字段是什么类型的? -
您应该使用 versionCode 而不是 versionName。 versionName 是一个字符串,versionCode 是一个整数。
-
如何解决请解释@AlexShevelev
标签: android android-studio exception