我发现了从webview
阅读内容的问题。我尝试过无法修复。
我在webview
中显示了一个html页面。我需要string
java代码
WebView mainContent = (WebView)layout.findViewById(R.id.webView1);
mainContent.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = mainContent.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
mainContent.requestFocusFromTouch();
mainContent.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mainContent.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
}
});
mainContent.setWebChromeClient(new WebChromeClient());
mainContent.loadUrl("file:///android_asset/"+filename.get(position));
webSettings.setDefaultFontSize(40);
在我的活动中
public void processHTML(String html) {
System.out.println("======++++"+Html.fromHtml(html));
}
我没有看到我的任何记录。如何在HTML
中获取String
页面的内容(文本)。我做错了什么?提前致谢
答案 0 :(得分:3)
我通过这种方式获得了内容
mainContent.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = mainContent.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
mainContent.requestFocusFromTouch();
mainContent.setWebChromeClient(new WebChromeClient());
mainContent.loadUrl("file:///android_asset/"+filename.get(position));
mainContent.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mainContent.setWebViewClient(null);
mainContent.loadUrl("javascript:window.HTMLOUT.processHTML('<div>'+document.getElementsByTagName('div')[0].innerHTML+'</div>');");
和我的活动
class MyJavaScriptInterface {
@SuppressWarnings("unused")
public void processHTML(final String html) {
runOnUiThread(new Runnable() {
public void run() {
Spanned page = Html.fromHtml(html);
System.out.println("content"+page);
}
});
}
}
答案 1 :(得分:1)
您是否尝试过向该页面发送GET请愿书?
responseString
是String中的HTML页面。例如:
class RequestTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//Do anything with response..
}
}
启动AsyncTask,如:
try {
String page = new RequestTask().execute("http://myurl.com").get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}