Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chayanforyou/e109efe303fff16c33d6be6d7ff7ff0a to your computer and use it in GitHub Desktop.
Save chayanforyou/e109efe303fff16c33d6be6d7ff7ff0a to your computer and use it in GitHub Desktop.

It is a library that receives and returns the version of the application uploaded to the market. If it is different from the current version, you can write the code so that it can be updated after moving to the market.

We refine some methods on the Internet and distribute them in the form of a library. The original java source is also attached.

Be careful

How to parse and fetch the Google Market website It takes a lot of data I don't really recommend using this library to check the version every time you run the app. It took 0.02mb to 0.04mb to import once in my tests.

Note that you must add Internet permission.

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

Library download

You need jsoup library to use MarketVersionChecker The jsoup library can be downloaded from http://jsoup.org/download and is attached below.

How to use

Not declared static So you have to use it after declaring new

MarketVersionChecker mChecker = new MarketVersionChecker();

There are two APIs to get the market version.

  • getMarketVersion(String packageName)
  • getMarketVersionFast(String packageName)

If you pass the packageName to get the version, it takes about 0.5-2 seconds on WIFI and about 1-3-4 seconds on 3G. As you can see from the method name, it was confirmed that the method with Fast below was faster in terms of speed. However, the actual development speed may vary, so please choose one of the two.

Speed test result

getMarketVersion : 1 to 4 seconds on Wi-Fi, 2-4.5 seconds on 3G getMarketVersionFast : 0.6 seconds to 1.5 seconds on Wi-Fi, 1-3 seconds on 3G

MarketVersionChecker.java

package me.chayan.marketversionchecker;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class MarketVersionChecker {

	public String getMarketVersion(String packageName) {
		try {
			Document doc = Jsoup.connect(
					"https://play.google.com/store/apps/details?id="
							+ packageName).get();
			Elements Version = doc.select(".content");

			for (Element mElement : Version) {
				if (mElement.attr("itemprop").equals("softwareVersion")) {
					return mElement.text().trim();
				}
			}

		} catch (IOException ex) {
			ex.printStackTrace();
		}

		return null;
	}

	public String getMarketVersionFast(String packageName) {
		String mData = "", mVer = null;

		try {
			URL mUrl = new URL("https://play.google.com/store/apps/details?id="
					+ packageName);
			HttpURLConnection mConnection = (HttpURLConnection) mUrl
					.openConnection();

			if (mConnection == null)
				return null;

			mConnection.setConnectTimeout(5000);
			mConnection.setUseCaches(false);
			mConnection.setDoOutput(true);

			if (mConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				BufferedReader mReader = new BufferedReader(
						new InputStreamReader(mConnection.getInputStream()));

				while (true) {
					String line = mReader.readLine();
					if (line == null)
						break;
					mData += line;
				}

				mReader.close();
			}

			mConnection.disconnect();

		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}

		String startToken = "softwareVersion\">";
		String endToken = "<";
		int index = mData.indexOf(startToken);

		if (index == -1) {
			mVer = null;

		} else {
			mVer = mData.substring(index + startToken.length(), index
					+ startToken.length() + 100);
			mVer = mVer.substring(0, mVer.indexOf(endToken)).trim();
		}

		return mVer;
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment