[英]Extract HTML content from a POST webview - Java
我正在尝试从Webview中提取HTML内容。 我在stackoverflow上发现了一个有趣的主题,但是所有这些答案都加载URL以获取HTML内容。 在这里,我需要提取通过POST方法生成的网页的HTML内容。 使用下面的java方法,将仅加载HTML内容(因为它在方法中加载了url,而不是直接从webview中提取html内容)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
private static class RetrieveHTML extends AsyncTask<String, String, String> {
private static String htmlContent;
protected String doInBackground(String... url) {
return getRemoteContent(url[0]);
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Long result) {
}
private static String getRemoteContent(String url)
{
HttpPost pageGet = new HttpPost(url);
HttpClient client = new DefaultHttpClient();
ResponseHandler<String> handler = new ResponseHandler<String>()
{
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException
{
HttpEntity entity = response.getEntity();
String html;
if (entity != null)
{
html = EntityUtils.toString(entity);
return html;
}
else
{
return null;
}
}
};
String pageHTML = null;
try
{
pageHTML = client.execute(pageGet, handler);
//if you want to manage http sessions then you have to add localContext as a third argument to this method and have uncomment below line to sync cookies.
//syncCookies();
}
catch (Exception e)
{
e.printStackTrace();
}
// you can filter your html content here if you wish before displaying
// in webview
try {
Log.d("TEST", pageHTML);
}
catch (Exception e){
e.printStackTrace();
}
htmlContent = pageHTML;
return pageHTML;
}
}
提前致谢
编辑:我忘了说为什么我要这样做:我正在将桌面网站改编为android应用程序(主要显示移动模板的webview)。 我在桌面网站上有一张地图,并在上面放置了标记(这些标记通过Flask + jinja通过json字符串传输)。 我想到了将那些标记隐藏在html隐藏标记中的想法。 然后,我可以提取html,然后解析此html内容的正确部分,以便将此json字符串获取到我的Java应用程序中(然后,使用android studio中存在的google-maps方法)
最终,我决定以另一种方式做自己想做的事。 每当我执行此发布请求时,我都会生成一个temp html文件,在其中写入需要在Java应用程序中获取的所有信息。 然后,我可以从Java调用此页面(使用上述方法),因为没有要重新发送的数据(因为它不是后生成的页面)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.