How to get app market version information from google play store?

后端 未结 15 1506
孤独总比滥情好
孤独总比滥情好 2020-11-29 00:33

How can I get the application version information from google play store for prompting the user for force/recommended an update of the application when play store applicatio

相关标签:
15条回答
  • 2020-11-29 00:47

    I recomned dont use a library just create a new class

    1.

    public class VersionChecker extends AsyncTask<String, String, String>{
    
    String newVersion;
    
    @Override
    protected String doInBackground(String... params) {
    
        try {
            newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + "package name" + "&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()
                    .select("div.hAyfc:nth-child(4) > span:nth-child(2) > div:nth-child(1) > span:nth-child(1)")
                    .first()
                    .ownText();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        return newVersion;
    }
    
    1. In your activity:

          VersionChecker versionChecker = new VersionChecker();
          String latestVersion = versionChecker.execute().get();
      

    THAT IS ALL

    0 讨论(0)
  • 2020-11-29 00:50

    Firebase Remote Config can help best here,

    Please refer this answer https://stackoverflow.com/a/45750132/2049384

    0 讨论(0)
  • 2020-11-29 00:50

    Full source code for this solution: https://stackoverflow.com/a/50479184/5740468

    import android.os.AsyncTask;
    import android.support.annotation.Nullable;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.util.regex.PatternSyntaxException;
    
    public class GooglePlayAppVersion extends AsyncTask<String, Void, String> {
    
        private final String packageName;
        private final Listener listener;
        public interface Listener {
            void result(String version);
        }
    
        public GooglePlayAppVersion(String packageName, Listener listener) {
            this.packageName = packageName;
            this.listener = listener;
        }
    
        @Override
        protected String doInBackground(String... params) {
            return getPlayStoreAppVersion(String.format("https://play.google.com/store/apps/details?id=%s", packageName));
        }
    
        @Override
        protected void onPostExecute(String version) {
            listener.result(version);
        }
    
        @Nullable
        private static String getPlayStoreAppVersion(String appUrlString) {
            String
                  currentVersion_PatternSeq = "<div[^>]*?>Current\\sVersion</div><span[^>]*?>(.*?)><div[^>]*?>(.*?)><span[^>]*?>(.*?)</span>",
                  appVersion_PatternSeq = "htlgb\">([^<]*)</s";
            try {
                URLConnection connection = new URL(appUrlString).openConnection();
                connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
                try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
                    StringBuilder sourceCode = new StringBuilder();
                    String line;
                    while ((line = br.readLine()) != null) sourceCode.append(line);
    
                    // Get the current version pattern sequence
                    String versionString = getAppVersion(currentVersion_PatternSeq, sourceCode.toString());
                    if (versionString == null) return null;
    
                    // get version from "htlgb">X.X.X</span>
                    return getAppVersion(appVersion_PatternSeq, versionString);
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        @Nullable
        private static String getAppVersion(String patternString, String input) {
            try {
                Pattern pattern = Pattern.compile(patternString);
                if (pattern == null) return null;
                Matcher matcher = pattern.matcher(input);
                if (matcher.find()) return matcher.group(1);
            } catch (PatternSyntaxException e) {
                e.printStackTrace();
            }
            return null;
        }
    
    }
    

    Usage:

    new GooglePlayAppVersion(getPackageName(), version -> 
        Log.d("TAG", String.format("App version: %s", version)
    ).execute();
    
    0 讨论(0)
  • 2020-11-29 00:50

    You can call the following WebService: http://carreto.pt/tools/android-store-version/?package=[YOUR_APP_PACKAGE_NAME]

    Example Using Volley:

    String packageName = "com.google.android.apps.plus";
    String url = "http://carreto.pt/tools/android-store-version/?package=";
    JsonObjectRequest jsObjRequest = new JsonObjectRequest
        (Request.Method.GET, url+packageName, null, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            /*
                                    here you have access to:
    
                                    package_name, - the app package name
                                    status - success (true) of the request or not (false)
                                    author - the app author
                                    app_name - the app name on the store
                                    locale - the locale defined by default for the app
                                    publish_date - the date when the update was published
                                    version - the version on the store
                                    last_version_description - the update text description
                                 */
                            try{
                                if(response != null && response.has("status") && response.getBoolean("status") && response.has("version")){
                                    Toast.makeText(getApplicationContext(), response.getString("version").toString(), Toast.LENGTH_LONG).show();
                                }
                                else{
                                    //TODO handling error
                                }
                            }
                            catch (Exception e){
                                //TODO handling error
                            }
    
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            //TODO handling error
                        }
            });
    
    0 讨论(0)
  • 2020-11-29 00:50

    User Version Api in Server Side:

    This is the best way still now to get the market version. When you will upload new apk, update the version in api. So you will get the latest version in your app. - This is best because there is no google api to get the app version.

    Using Jsoup Library:

    This is basically web scraping. This is not a convenient way because if google changes their code This process will not work. Though the possiblity is less. Anyway, To get the version with Jsop library.

    1. add this library in your build.gradle

      implementation 'org.jsoup:jsoup:1.11.1'

    2. Creat a class for version check:

    import android.os.AsyncTask import org.jsoup.Jsoup import java.io.IOException

    class PlayStoreVersionChecker(private val packageName: String) : AsyncTask() {

    private var playStoreVersion: String = ""
    
    override fun doInBackground(vararg params: String?): String {
        try {
            playStoreVersion =
                    Jsoup.connect("https://play.google.com/store/apps/details?id=$packageName&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()
                        .select("div.hAyfc:nth-child(4) > span:nth-child(2) > div:nth-child(1) > span:nth-child(1)")
                        .first()
                        .ownText()
        } catch (e: IOException) {
        }
        return playStoreVersion
    } }
    
    1. Now use the class as follows:

      val playStoreVersion = PlayStoreVersionChecker("com.example").execute().get()

    0 讨论(0)
  • 2020-11-29 00:56

    I will recommend to use ex. push notification to notify your app that there is a new update, OR use your own server to enable your app read version from there.

    Yes its additional work each time you update your app, but in this case your are not depended on some "unofficial" or third party things that may run out of service.

    Just in case you missed something - previous discussion of your topic query the google play store for the version of an app?

    0 讨论(0)
提交回复
热议问题