从Play商店获取当前应用程序版本

3

我想开发 Android 应用程序;我需要检查 Play Store 中是否有比当前版本更高的版本。

我尝试编写以下代码:

try {
    URL updateURL = new URL("https://play.google.com/store/apps/details?id=com.XXXX.YYYY");                
    URLConnection conn = updateURL.openConnection();
    InputStream is = conn.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);
    ByteArrayBuffer baf = new ByteArrayBuffer(50);

    int current = 0;
    while((current = bis.read()) != -1){
        baf.append((byte)current);
    }

    /* Convert the Bytes read to a String. */
    final String s = new String(baf.toByteArray());         

    /* Get current Version Number */
    int curVersion = getPackageManager().getPackageInfo("com.XXXX.YYYY", 0).versionCode;
    int newVersion = Integer.valueOf(s);

    /* Is a higher version than the current already out? */
    if (newVersion > curVersion) {
        /* Post a Handler for the UI to pick up and open the Dialog */
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:com.XXXX.YYYY"));
        startActivity(intent);
    }                
} catch (Exception e) {
    Toast.makeText(getApplicationContext(), e.getStackTrace().toString(), Toast.LENGTH_LONG).show();    
}

但是这段代码的错误在于:
java.net.UnknownHostException: Unable to resolve host "play.google.com": No address associated with hostname
Unable to resolve host "play.google.com": No address associated with hostname
2个回答

1
似乎您的网络连接存在问题。同时请确保在您的AndroidManifest.xml中已经声明了互联网访问权限。
<uses-permission android:name="android.permission.INTERNET" />

您上面展示的代码很令人困惑。您如何以这种方式获取版本号?此请求
URL updateURL = new URL("https://play.google.com/store/apps/details?id=com.XXXX.YYYY");

将返回您在Google Play中应用程序的HTML内容。您无法在没有进一步处理的情况下获取版本信息。

你能帮我学习如何获取应用程序版本或从Play商店更新我的应用程序吗? - Mohammad Rababah
您不必担心更新问题。如果您的应用程序已发布到Google Play,并且有新版本可用,Google Play将处理此更新,向用户显示通知或自动更新(如果将您的应用设置为自动更新)。如果您坚持这样做,可以使用一些Java HTML解析库从返回的HTML内容中获取版本信息。 - alijandro

0
doc.getElementsByAttributeValue
                        ("itemprop", "softwareVersion").first().text();

软件版本不可用,请尝试下面的代码

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

            @Override
            protected String doInBackground(Void... voids) {
                String newVersion = null;
                try {
                    Connection connection = Jsoup.connect(Config.APP_UPDATE_URL + "&hl=en");
                    Document document = connection.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();

                    Elements versions = document.getElementsByClass("htlgb");

                    for (int i = 0; i < versions.size(); i++) {
                        newVersion = versions.get(i).text();
                        if (Pattern.matches("^[0-9]{1}.[0-9]{1}.[0-9]{1}$", newVersion)) {
                            break;
                        }
                    }

                } catch (Exception e) {
                    return newVersion;
                }
                return newVersion;
            }

            @Override
            protected void onPostExecute(String onlineVersion) {
                super.onPostExecute(onlineVersion);
                if (onlineVersion != null && !onlineVersion.isEmpty()) {
                    if (!onlineVersion.equalsIgnoreCase(currentVersionName)) {
                        startActivity(new Intent(SplashActivity.this, UpdateVersionActivity.class));
                        finish();
                    } else {
                        callPermission();
                    }
                } else {
                    callPermission();
                }
            }
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接