如何将文档转换为字符串而无例外?

发布于 2025-01-27 05:10:13 字数 1238 浏览 1 评论 0原文

我使用 javascriptInterface jsoup url 中获取 html 字符串,然后加载到 WebView 在WebView中的单击元素上,但是问题是,单击后,应用程序崩溃并抛出异常“错误转换为字符串” 我尝试了许多答案如何将文档转换为 string ,但对我没有任何帮助。 请让我知道我的代码中有什么问题...

WebView urlview = findViewById(R.id.urlview);
String newsUrlToShare;

Thread HtmlParser = new Thread(){
        public void run(){
            try {
                Document doc = Jsoup.connect(newsUrlToShare).followRedirects(false).timeout(0).method(Connection.Method.GET).get();
                String body = doc.html();
                urlview.loadDataWithBaseURL("https://google.com",body,"text/html","utf-8",null);
            }catch(Exception ex) {
                throw new RuntimeException("Error Converting to String",ex);
            }
        }
};


private final class jsInterface{

        @JavascriptInterface
        public final void openNews(String url){

            urlview.post(() -> {
                if(!url.equals("")){

                    BrowserOpened = false;
                    newsUrlToShare = url;

                    HtmlParser.start();

                }
            });
        }
}

I use JavascriptInterface and JSOUP to get HTML string from URL and load into WebView on click element inside webView, but problem is that after click app is crashing and throw in exception "Error Converting to String"
I tried many answears how to convert Document to String but nothing worked for me.
Please let me know what is wrong in my code...

WebView urlview = findViewById(R.id.urlview);
String newsUrlToShare;

Thread HtmlParser = new Thread(){
        public void run(){
            try {
                Document doc = Jsoup.connect(newsUrlToShare).followRedirects(false).timeout(0).method(Connection.Method.GET).get();
                String body = doc.html();
                urlview.loadDataWithBaseURL("https://google.com",body,"text/html","utf-8",null);
            }catch(Exception ex) {
                throw new RuntimeException("Error Converting to String",ex);
            }
        }
};


private final class jsInterface{

        @JavascriptInterface
        public final void openNews(String url){

            urlview.post(() -> {
                if(!url.equals("")){

                    BrowserOpened = false;
                    newsUrlToShare = url;

                    HtmlParser.start();

                }
            });
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文