简体   繁体   中英

Extract HTML content from a POST webview - Java

I am trying to extract the HTML content from a Webview. I found interesting subject on stackoverflow, but all of these answers loads the URL in order to get the HTML content. Here, I need to extract the HTML content of a webpage that has been generated from a POST method. 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)

<!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). 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). I got the idea to hide thoses markers in an html hidden tag. 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)

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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