I have this class to check if any available update in playsore it uses Jsoup
, everything was working on debug
but when i upload release
version it will crash i don't really know what is the problem. Please can anyone assist me?
Fatal Exception: java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:365) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383) at java.util.concurrent.FutureTask.setException(FutureTask.java:252) at java.util.concurrent.FutureTask.run(FutureTask.java:271) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:784)
Caused by java.lang.ExceptionInInitializerError at org.jsoup.nodes.Entities.access$000(Entities.java:1) at org.jsoup.nodes.Entities$EscapeMode.(Entities.java:1) at org.jsoup.nodes.Document$OutputSettings.(Document.java:3) at org.jsoup.nodes.Document.(Document.java:11) at org.jsoup.parser.TreeBuilder.a(TreeBuilder.java:12) at org.jsoup.parser.TreeBuilder.runParser(TreeBuilder.java) at org.jsoup.parser.Tokeniser.acknowledgeSelfClosingFlag(Tokeniser.java:7) at org.jsoup.parser.HtmlTreeBuilder.insertEmpty(HtmlTreeBuilder.java:7) at org.jsoup.parser.Parser.parseInput(Parser.java:5) at org.jsoup.helper.DataUtil.parseByteData(DataUtil.java:6) at org.jsoup.helper.HttpConnection$Response.parse(HttpConnection.java:7) at org.jsoup.helper.HttpConnection.get(HttpConnection.java:7) at com.square.android.buyer.GetVersionCode.doInBackground(GetVersionCode.java:7) at android.os.AsyncTask$2.call(AsyncTask.java:345) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:784)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.Reader.read(char[])' on a null object reference at java.util.Properties$LineReader.readLine(Properties.java:432) at java.util.Properties.load0(Properties.java:348) at java.util.Properties.load(Properties.java:336) at org.jsoup.nodes.Entities.a(Entities.java:16) at org.jsoup.nodes.Entities.(Entities.java:82) at org.jsoup.nodes.Entities.access$000(Entities.java:1) at org.jsoup.nodes.Entities$EscapeMode.(Entities.java:1) at org.jsoup.nodes.Document$OutputSettings.(Document.java:3) at org.jsoup.nodes.Document.(Document.java:11) at org.jsoup.parser.TreeBuilder.a(TreeBuilder.java:12) at org.jsoup.parser.TreeBuilder.runParser(TreeBuilder.java) at org.jsoup.parser.Tokeniser.acknowledgeSelfClosingFlag(Tokeniser.java:7) at org.jsoup.parser.HtmlTreeBuilder.insertEmpty(HtmlTreeBuilder.java:7) at org.jsoup.parser.Parser.parseInput(Parser.java:5) at org.jsoup.helper.DataUtil.parseByteData(DataUtil.java:6) at org.jsoup.helper.HttpConnection$Response.parse(HttpConnection.java:7) at org.jsoup.helper.HttpConnection.get(HttpConnection.java:7) at com.square.android.buyer.GetVersionCode.doInBackground(GetVersionCode.java:7) at android.os.AsyncTask$2.call(AsyncTask.java:345) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:784)
public class GetVersionCode extends AsyncTask<Void, String, String> {
private Activity mActivity;
private String currentVersion;
private String packageName;
private boolean isForceUpdate;
public GetVersionCode(Activity act, String version, boolean forceUpdate) {
this.mActivity = act;
this.currentVersion = version;
this.isForceUpdate = forceUpdate;
this.packageName = act.getPackageName();
}
@Override
protected String doInBackground(Void... voids) {
String newVersion = null;
try {
Document document = 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(Utils.HOST_NAME)
.get();
if (document != null) {
Elements element = document.getElementsContainingOwnText("Current Version");
for (Element ele : element) {
if (ele.siblingElements() != null) {
Elements sibElemets = ele.siblingElements();
for (Element sibElemet : sibElemets) {
newVersion = sibElemet.text();
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return newVersion;
}
@Override
protected void onPostExecute(String onlineVersion) {
super.onPostExecute(onlineVersion);
if (onlineVersion != null && !onlineVersion.isEmpty()) {
Log.d("update", "Current version " + currentVersion + " playstore version " + onlineVersion);
// if(Float.valueOf(currentVersion) < Float.valueOf(onlineVersion)) {
if(!currentVersion.equals(onlineVersion)) {
//show anything
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity, R.style.AlertDialogStyle);
String alertMessage = Utils.APP_NAME + " Version " + onlineVersion + " is available on PlayStore.";
alertDialogBuilder.setTitle("New Version");
alertDialogBuilder.setMessage( alertMessage );
alertDialogBuilder.setPositiveButton("UPDATE", (dialog, which) -> {
dialog.cancel();
try {
mActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName)));
} catch (ActivityNotFoundException anfe) {
mActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName)));
}
});
if(!isForceUpdate) {
alertDialogBuilder.setNegativeButton("NOT NOW", (dialog, which) -> {
dialog.cancel();
});
}else{
alertDialogBuilder.setCancelable(false);
}
alertDialogBuilder.create().show();
}
}
}
}
Usage
checkForUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
GetVersionCode gvc = new GetVersionCode(MainActivity.this, BuildConfig.VERSION_NAME, true);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
gvc.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
gvc.execute();
}
}
});
}
});
at org.jsoup.nodes.Entities$EscapeMode.(Entities.java:1)
It reminds me there was a problem with loading properties files as Android resources and class EscapeMode
was using these.
It was fixed in Jsoup issue #959, fix released in version 1.11.1 so try using at least this version.