[英]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.