gpt4 book ai didi

Android 网页 View Jsoup

转载 作者:行者123 更新时间:2023-11-30 00:55:10 25 4
gpt4 key购买 nike

我正在开发一个应用程序,我想在其中显示我的网站,但使用较少的 div 元素。

在此示例代码中,我使用了 stackoverflow.com 网站并尝试隐藏 div 类 hmenus(主网站顶部的按钮)。

运行代码后,我得到一个空白/白色的 webview。我尝试的是使 String html 最终化,然后调用它来为 webview 设置我的最终 url。

代码:

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.File;

public class MyCustomAsyncTask extends AsyncTask<File, Void, String> {
@Override
protected String doInBackground(File... params) {

Document doc = Jsoup.connect("http://stackoverflow.com/").get();
Elements ele = doc.select(":not(#hmenus)"); // ->>>>> Jsoup html
final String html = ele.toString();

}}

public class MainActivity extends Activity {


private WebView WebView;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView = (WebView) findViewById(webView);
WebView.setWebChromeClient(new WebChromeClient());
WebView.getSettings().setJavaScriptEnabled(true);
WebView.getSettings().setDomStorageEnabled(true);

final String mime = "text/html";
final String encoding = "utf-8";
WebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {

// ->>>>>> Change the website --->>> insert call html value
WebView.loadData(html, mime, encoding);
}
});
}
}

如何调用 html final String html = ele.toString(); 的值并将其转换为 WebView.loadData(html, mime, encoding);

最佳答案

您没有以正确的方式使用 AsyncTask。你应该阅读 this .

声明 AsyncTask 并实现 doInBackground 以获取网页和 onPostExecute 以在准备好时显示结果:

private class GetData extends AsyncTask<Void, Void, String> {

// This is run in a background thread
@Override
protected String doInBackground(Void... params) {
try {
Document doc = Jsoup.connect("https://stackoverflow.com/")
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0")
.get();
Elements ele = doc.select("#hmenus");
html = ele.toString();
return html;
} catch (Exception e) {
Log.d("APP", e.toString());
}
return "error";
}

// This runs in UI when background thread finishes
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d("APP", "1");
WebView.loadData(result, mime, encoding);
}
}

至于MainActivity,它必须调用AsyuncTask:

public class MainActivity extends Activity {

private WebView WebView;
String html = "Loading...";
final String mime = "text/html";
final String encoding = "utf-8";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView = (WebView) findViewById(R.id.webview);
WebView.setWebChromeClient(new WebChromeClient());
WebView.getSettings().setJavaScriptEnabled(true);
WebView.getSettings().setDomStorageEnabled(true);
//Display "Loading..." message while waiting
WebView.loadData(html, mime, encoding);
//Invoke the AsyncTask
new GetData().execute();
}
}

不要忘记 list 中的 internet-permission

关于Android 网页 View Jsoup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40296814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com