Exploring

Querying Google Play from your latest version of your Android app

noras noras |

Tags: #Android #Java

Few weeks ago, I needed a way to show if there is an a newer version of an app on Google’s play store. I needed a solution that does not require me having a server setup, where I have to keep a value of the latest version. Everything needs to be dynamic. I found a way to do that by fetching the app’s page on google play and parse the output.

Prerequisites: In the next code snippet I use a APIClient. This is your normal HTTP client on Android and can be anything for example: Google’s Volley library.

Code snippet

 public void isLatestVersion(){
        String url="https://play.google.com/store/apps/details?id="+getPackageName()+"&hl=en";


        APIClient client = new APIClient(url);
        client.get( url, null,  new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                super.onSuccess(statusCode, headers, responseBody);
                String response=new String(responseBody);

                String patternString="<div class=\"[A-Za-z0-9]+\"><div class=\"[A-Za-z0-9]+\">Current Version</div><span class=\"[A-Za-z0-9]+\"><div><span class=\"[A-Za-z0-9]+\">([0-9]+[.]{0,1})+";
                Pattern pattern = Pattern.compile(patternString);
                Matcher matcher = pattern.matcher(response);
                if(matcher.find()){
                    Log.d("r",response);
                    Log.d("r",matcher.start()+"");
                    Log.d("r",matcher.end()+"");
                    String regexmatch=response.substring(matcher.start(),matcher.end());
                    String version=regexmatch.substring(regexmatch.lastIndexOf('>')+1);
                    Log.d("r",version);
                    Log.d("r", BuildConfig.VERSION_NAME);

                }

               // Log.d("r",response);
            }


            @Override
            public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
                Timber.e(throwable, "HTTP statusCode: " + statusCode);

                Toast.makeText(getApplicationContext(), getString(R.string.msg_unknown_error_connection_required), Toast.LENGTH_SHORT).show();
            }
        });
    }

About the author

noras

"Senior Software Engineer. MSc in Computer systems and Networks with big interest in security. Loves to play with Android code and does security research for fun and profit. Speaks 4 languages and codes in much more."

Related articles

Tags: #Android #Java




Copyright © 2019 - nindoda.com