繁体   English   中英

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

[英]Android AsyncTask with Jsoup crashes on release version

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

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:78 4)

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(HttpCo nnection.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) 在 java.lang.Thread.run(Thread.java:78)

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$OutputSettin gs.(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.Z93F725A07423FE1C889F 448B33D21F46Z: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();
            }
        }
    }
}

用法

 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 问题 #959中修复,在 1.11.1 版本中发布,因此请尝试至少使用此版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM