Issue
My current code works perfectly fine to get current version from google store, But is theres away to get both current version and whats new in the same response ? I want to recommend the user an update if the application is updated on play store and the user still using an older version. Here's my code below
public class AppVersion extends AsyncTask<String, Void, String> {
private final String packageName;
private final Listener listener;
public interface Listener {
void result(String version);
}
AppVersion(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;
}
}
and to call the method i use
new AppVersion(context.getPackageName(), version ->
Log.d("app version test", String.format("App version: %s", version)
)).execute();
Solution
In my opinion this is a bad design for your App. You should not rely on scraping to Play store to show a prompt to the user to upgrade. What if the Play Store decides to change design of their HTML? You could leave all your users in a buggy situation.
A much better solution would be to use something like Firebase Remote Config. Have your app read the minimum allowed version code from this. Then it is completely controllable by you, and you don't even have to write a server - firebase does it for you.
Answered By - Nick Fortescue
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.