App版本更新与Play商店的检查无需使用任何后端服务器/网络服务



如何在Play商店中查看新更新。是否有任何简单的方法来获得此方法。

限制。 1.没有自定义网络服务。

可以使用

  1. 用户代理。
  2. Google提供的任何API。

任何帮助将不胜感激。

使用此助手方法在Play Store中查找更新版本

private String getPlayStoreVersion(){
    final String PLAY_STORE_URL = "https://play.google.com/store/apps/details?id=%s&hl=%s";
    final String PLAY_STORE_TAG_RELEASE = "itemprop="softwareVersion">";
    String version = "0.0.0.0";
    Boolean isAvailable = false;
    String source = "";
    OkHttpClient client = new OkHttpClient();
    String formedString = String.format(PLAY_STORE_URL,"com.demo.application.yourapp",Locale.getDefault().getLanguage());
    ResponseBody body = null;
    try {
        URL playStoreURL = new URL(formedString);
        Request request = new Request.Builder().url(url).build();
        Response response = client.newCall(request).execute();
        body = response.body();
        BufferedReader reader = new BufferedReader(new InputStreamReader(body.byteStream(), "UTF-8"));
        StringBuilder str = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.contains(PLAY_STORE_TAG_RELEASE)) {
                str.append(line);
                isAvailable = true;
               }
        }
        response.body().close();
        source = str.toString();
        if (isAvailable) {
            String[] splitPlayStore = source.split(PLAY_STORE_TAG_RELEASE);
            if (splitPlayStore.length > 1) {
                splitPlayStore = splitPlayStore[1].split("(<)");
                version = splitPlayStore[0].trim();
            }
         }

        return version;
    } catch(Exception e){
        e.printStackTrace();
    }

    return version;
}  

注意:在背景线程中使用

,所以我认为我很容易解决这个问题。

需要依赖。

compile 'org.jsoup:jsoup:1.7.3'

示例代码

class GetVersionCode extends AsyncTask<Void, String, String> {
    private static final String currentVersion = "x.x.xx";
    static final String packageName = "com.xxx.xxx";
    @Override
    protected String doInBackground(Void... voids) {
        String newVersion = null;
        try {
            newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + packageName + "&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) {
            // HTTP error fetching URL, 404
            return newVersion;
        }
    }
    @Override
    protected void onPostExecute(String onlineVersion) {
        super.onPostExecute(onlineVersion);
        if (null == onlineVersion) {
            // Some error occurred
            return;
        }
        if (onlineVersion.compareTo("") == 0) {
            // Some error occurred
            return;
        }
        Toast.makeText(getApplicationContext(), onlineVersion, Toast.LENGTH_SHORT).show();
    }
}

最新更新