Android AsyncTask with Jsoup 在发布版本上崩溃

Android AsyncTask with Jsoup crashes on release version

我有这个 class 来检查它使用 Jsoup 的 playsore 中是否有任何可用更新,一切都在 debug 上工作但是当我上传 release 版本时它会崩溃我真的不知道是什么问题。请问有人可以帮助我吗?

Fatal Exception: java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask.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[=16=]0(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.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[=16=]0(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.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();
            }
        }
    }
}

用法

 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)

它提醒我加载属性文件作为 Android 资源时出现问题,class EscapeMode 正在使用这些资源。 它已在 Jsoup issue #959 中修复,修复在版本 1.11.1 中发布,因此请尝试至少使用此版本。