3

I am trying to develop android app; I need to check Is a higher version than the current already out -From play store- ?

I tried to write this code:

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();    
}

but error in this code is:

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
Mohammad Rababah
  • 1,730
  • 4
  • 17
  • 32

2 Answers2

1

It seems there are problems in your network connection. Also make sure internet access permission has already been declared in your AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

The code you showed above is confusing. How can you get the version number in this way? This request

URL updateURL = new URL("https://play.google.com/store/apps/details?id=com.XXXX.YYYY");

will return the HTML content of your app in Google play. You can't get the version without further processing.

Community
  • 1
  • 1
alijandro
  • 11,627
  • 2
  • 58
  • 74
  • can you help me how to get app version or update my app from play store? – Mohammad Rababah Jan 13 '15 at 12:26
  • You don't have to worry the update. If you app has been published to Google play, and there is a newer version available, Google play will handle this update, show notification to user or update automatically if the set your app as automated update. If you insist on this, you can use some java html parser library to get the version from the returning html content. – alijandro Jan 13 '15 at 13:11
0
doc.getElementsByAttributeValue
                        ("itemprop", "softwareVersion").first().text();

softwareVersion is not available so try this code below

class GetVersionCode extends AsyncTask<Void, String, String> {

            @Override
            protected String doInBackground(Void... voids) {
                String newVersion = null;
                try {
                    Connection connection = Jsoup.connect(Config.APP_UPDATE_URL + "&hl=en");
                    Document document = connection.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();

                    Elements versions = document.getElementsByClass("htlgb");

                    for (int i = 0; i < versions.size(); i++) {
                        newVersion = versions.get(i).text();
                        if (Pattern.matches("^[0-9]{1}.[0-9]{1}.[0-9]{1}$", newVersion)) {
                            break;
                        }
                    }

                } catch (Exception e) {
                    return newVersion;
                }
                return newVersion;
            }

            @Override
            protected void onPostExecute(String onlineVersion) {
                super.onPostExecute(onlineVersion);
                if (onlineVersion != null && !onlineVersion.isEmpty()) {
                    if (!onlineVersion.equalsIgnoreCase(currentVersionName)) {
                        startActivity(new Intent(SplashActivity.this, UpdateVersionActivity.class));
                        finish();
                    } else {
                        callPermission();
                    }
                } else {
                    callPermission();
                }
            }
        }