在 Android 市场中以编程方式检查我的应用程序版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12091534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:42:53  来源:igfitidea点击:

Checking my app version programmatically in Android market

androidgoogle-playauto-update

提问by mrYogi

Currently I'm checking the app version code on launch and match it with latest version code on my server and based on this matching I send user to get latest update from Android market.

目前我正在启动时检查应用程序版本代码并将其与我服务器上的最新版本代码匹配,并根据此匹配我发送用户以从 Android 市场获取最新更新。

It's working well but my problem is that I have to manually change the latest version code on my server and I don't know exactly when new version of APKgets activated in market.

它运行良好,但我的问题是我必须手动更改服务器上的最新版本代码,我不知道新版本何时APK在市场上激活。

Is there any way to check version code of app in Android market directly so that I can send user directly to market when new apk gets activated?

有什么方法可以直接在Android市场上查看应用程序的版本代码,以便在新的apk激活时我可以直接将用户发送到市场上?

采纳答案by Rajesh

Google Play does not provide any official APIs for retrieving metadata. You could however, check the unofficial API at http://code.google.com/p/android-market-api/.

Google Play 不提供任何用于检索元数据的官方 API。但是,您可以在http://code.google.com/p/android-market-api/ 上查看非官方 API 。

Specifically, take a look at the Wiki page HowToSearchApps. The response to the query contains version information:

具体来说,请查看 Wiki 页面HowToSearchApps。对查询的响应包含版本信息:

{
  "app": [
    {
      "rating": "4.642857142857143",
      "title": "Ruboto IRB",
      "ratingsCount": 14,
      "creator": "Jan Berkel",
      "appType": "APPLICATION",
      "id": "9089465703133677000",
      "packageName": "org.jruby.ruboto.irb",
      "version": "0.1",
      "versionCode": 1,
      "creatorId": "\"Jan Berkel\"",
      "ExtendedInfo": {
        "category": "Tools",
        "permissionId": [
...

回答by DRK

We can do pattern matching for getting the app version from playStore.

我们可以进行模式匹配以从 playStore 获取应用程序版本。

To match the latest pattern from google playstore ie <div class="BgcNfc">Current Version</div><span class="htlgb"><div><span class="htlgb">X.X.X</span></div>we first have to match the above node sequence and then from above sequence get the version value. Below is the code snippet for same:

要匹配来自 google playstore 的最新模式,即 <div class="BgcNfc">Current Version</div><span class="htlgb"><div><span class="htlgb">X.X.X</span></div>我们首先必须匹配上面的节点序列,然后从上面的序列中获取版本值。下面是相同的代码片段:

    private String getAppVersion(String patternString, String inputString) {
        try{
            //Create a pattern
            Pattern pattern = Pattern.compile(patternString);
            if (null == pattern) {
                return null;
            }

            //Match the pattern string in provided string
            Matcher matcher = pattern.matcher(inputString);
            if (null != matcher && matcher.find()) {
                return matcher.group(1);
            }

        }catch (PatternSyntaxException ex) {

            ex.printStackTrace();
        }

        return null;
    }


    private String getPlayStoreAppVersion(String appUrlString) {
        final String currentVersion_PatternSeq = "<div[^>]*?>Current\sVersion</div><span[^>]*?>(.*?)><div[^>]*?>(.*?)><span[^>]*?>(.*?)</span>";
        final String appVersion_PatternSeq = "htlgb\">([^<]*)</s";
        String playStoreAppVersion = null;

        BufferedReader inReader = null;
        URLConnection uc = null;
        StringBuilder urlData = new StringBuilder();

        final URL url = new URL(appUrlString);
        uc = url.openConnection();
        if(uc == null) {
           return null;
        }
        uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
        inReader = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        if (null != inReader) {
            String str = "";
            while ((str = inReader.readLine()) != null) {
                           urlData.append(str);
            }
        }

        // Get the current version pattern sequence 
        String versionString = getAppVersion (currentVersion_PatternSeq, urlData.toString());
        if(null == versionString){ 
            return null;
        }else{
            // get version from "htlgb">X.X.X</span>
            playStoreAppVersion = getAppVersion (appVersion_PatternSeq, versionString);
        }

        return playStoreAppVersion;
    }

I got this solved through this. Hope that helps.

我通过这个解决了这个问题。希望有帮助。