0

可能重复:
是否可以从 WebView 获取 HTML 代码

我正在尝试制作一个需要从网站获取 html 代码并通过代码查找图像的应用程序。我现在所在的位置在模拟器中给了我一个 html 代码,但是当我在我的计算机上打开网站的源代码时,它是一个不同的代码。

public String getInternetData(String adresse) throws Exception{
    BufferedReader in = null;
    String data = null;
    try{
        HttpClient client = new DefaultHttpClient();
        URI website = new URI(adresse);
        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String l = "";
        String nl = System.getProperty("line.separator");
        while ((l = in.readLine()) !=null){
            sb.append(l + nl);
        }
        in.close();
        data = sb.toString();
        return data;
    }finally{
        if (in != null){
            try{
                in.close();
                return data;
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

我在模拟器中获得的代码以需要启用 JavaScript 和 Cookies 结束。如果这是我的问题,我该如何解决?

任何帮助将非常感激!

4

2 回答 2

1
  1. 有时问题出在 UserAgent 中(服务器可以发送简化/移动版本的页面来查看您的 useragent 字符串)。尝试在您的代码中使用与浏览器相同的用户代理
  2. 尝试使用 HtmlUnit“无头浏览器”框架来执行 javascript。
于 2012-07-16T05:40:31.827 回答
0

请参阅此SO 线程上的Aymon Fournier的回答,

private String getDownloadButtonOnly(String url){
    HttpGet pageGet = new HttpGet(url);

    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;
            }
        }
    };

    pageHTML = null;
    try {
        while (pageHTML==null){
            pageHTML = client.execute(pageGet, handler);
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

        Pattern pattern = Pattern.compile("<h2>Direct Down.+?</h2>(</div>)*(.+?)<.+?>", Pattern.DOTALL);
        Matcher matcher = pattern.matcher(pageHTML);
        String displayHTML = null;
        while(matcher.find()){
            displayHTML = matcher.group();
        }

    return displayHTML;
}

    @Override
    public void customizeWebView(final ServiceCommunicableActivity activity, final WebView webview, final SearchResult mRom) {
        mRom.setFileSize(getFileSize(mRom.getURLSuffix()));
        webview.getSettings().setJavaScriptEnabled(true);
        WebViewClient anchorWebViewClient = new WebViewClient()
        {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                String downloadButtonHTML = getDownloadButtonOnly(url);
                if(downloadButtonHTML!=null && !url.equals(lastLoadedURL)){
                    lastLoadedURL = url;
                    webview.loadDataWithBaseURL(url, downloadButtonHTML, null, "utf-8", url);
                }
            }
于 2012-07-15T15:27:46.287 回答