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

标签 android google-play

我正在尝试开发安卓应用;我需要检查是否有比当前版本更高的版本 -From 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

最佳答案

您的网络连接似乎有问题。还要确保已经在您的 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 内容。未经进一步处理,您将无法获取版本。

关于android - 从 Play 商店获取当前应用程序版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27921795/

相关文章:

Android MSM 内核 : copy_to_user fails

android - 非市场内容发布

android - 哪个库正在为我的应用收集广告 ID?

android - 是否可以查询Google Play服务器以了解用户是否购买了某件商品?

java - 安卓。 onEditorAction 从未调用过

java - 自定义警报对话框中的访问按钮

安卓开发 : How to get a GeoPoint by LONG PRESS on MapView?

java - 如何将 RadioGroup 验证为必填字段?

android - 如果您受邀分享 Google Play Android 开发者控制台,那么如何将应用程序添加到向您发送邀请的管理员控制台。?