简体   繁体   English

从POST Webview提取HTML内容-Java

[英]Extract HTML content from a POST webview - Java

I am trying to extract the HTML content from a Webview. 我正在尝试从Webview中提取HTML内容。 I found interesting subject on stackoverflow, but all of these answers loads the URL in order to get the HTML content. 我在stackoverflow上发现了一个有趣的主题,但是所有这些答案都加载URL以获取HTML内容。 Here, I need to extract the HTML content of a webpage that has been generated from a POST method. 在这里,我需要提取通过POST方法生成的网页的HTML内容。 Using, the java method below, the HTML content loaded will just be (because it loads the url within the method, instead of directly extracting the html content from the webview) 使用下面的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;
    }

}

Thanks in advance 提前致谢

EDIT : I forgot to say why I am trying to do this : I am adapting a Desktop website into an android application (mostly showing webview of mobile templates). 编辑:我忘了说为什么我要这样做:我正在将桌面网站改编为android应用程序(主要显示移动模板的webview)。 I have a map into my desktop website, and markers are placed on it (those markers are transmitted via a json String through Flask+jinja). 我在桌面网站上有一张地图,并在上面放置了标记(这些标记通过Flask + jinja通过json字符串传输)。 I got the idea to hide thoses markers in an html hidden tag. 我想到了将那些标记隐藏在html隐藏标记中的想法。 I could then extract the html and then parse the right part of this html content in order to get this json string into my java application (and then, use google-maps method thats exists in android studio) 然后,我可以提取html,然后解析此html内容的正确部分,以便将此json字符串获取到我的Java应用程序中(然后,使用android studio中存在的google-maps方法)

Finally I decided to do what I wanted by an other way. 最终,我决定以另一种方式做自己想做的事。 Everytime I do this post request, i generate a temp html file in which I write all the information that I need to get within my Java Application. 每当我执行此发布请求时,我都会生成一个temp html文件,在其中写入需要在Java应用程序中获取的所有信息。 I can then call this page from java (using the method above) because there is no data to re-send (since it is not a post-generated page) 然后,我可以从Java调用此页面(使用上述方法),因为没有要重新发送的数据(因为它不是后生成的页面)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM