【问题标题】:How to get the HTML code of the page that would be loaded in webview?如何获取将在 webview 中加载的页面的 HTML 代码?
【发布时间】:2011-11-18 06:44:04
【问题描述】:

我有一个 android 应用程序,我需要在其中获取将在 webview 中加载的页面的 HTML 代码。这段代码我需要在页面加载到 webview 之前得到它并采取相应的行动。

我正在尝试捕获将在方法中加载的 URL

"public void onPageStarted(WebView view, String url, Bitmap favicon)"

但是获取将在 webview 中加载的页面的 HTML 代码的方法是什么?

【问题讨论】:

    标签: android html android-webview


    【解决方案1】:

    使用 HttpClient:

    public String httpGet(String url) throws URISyntaxException,
           ClientProtocolException, IOException {
          try {
    
           HttpGet request = new HttpGet();
    
           request.setURI(new URI(url));
           response = client.execute(request);
    
           htmlBody = EntityUtils.toString(response.getEntity());
    
          } catch (Exception ex) {
          }
    
          return htmlBody;
         }
    

    在 WebView 中显示它有两种方式,一种是从文件中显示(那么你应该在设备上保存一个带有 html 的文件,这使得内容可以离线使用):

    myWebView.loadUrl("file://"
                                    + Environment.getExternalStorageDirectory()
                                    + "/filename.html");
    

    或者你给 WebView 提供一个带有 html 的字符串:

    myWebView.loadData(HTMLString, "text/html", "utf-8");
    

    这里你可能会找到我写的整个httpClient,有些功能你可能需要,有些不需要:

    http://droidsnip.blogspot.com/2011/10/custom-http-client-android.html#more

    【讨论】:

      【解决方案2】:

      我不知道如何获取 webview 内容,但我将其用于网页代码

      HttpClient httpClient = new DefaultHttpClient();
      HttpContext localContext = new BasicHttpContext();
      HttpGet httpGet = new HttpGet("website url");
      HttpResponse response = httpClient.execute(httpGet, localContext);
      String result = "";
      
      BufferedReader reader = new BufferedReader(
          new InputStreamReader(
            response.getEntity().getContent()
          )
        );
      
      String line = null;
      while ((line = reader.readLine()) != null){
        result += line + "\n";
      
      }
      

      第二种方法是

       URL url;
          try {
              // url = new URL(data.getScheme(), data.getHost(), data.getPath());
              url = new URL("website url");
              BufferedReader rd = new BufferedReader(new InputStreamReader(
                      url.openStream()));
              String line = "";
              while ((line = rd.readLine()) != null) {
                  text.append(line);
              }
      
          } catch (Exception e) {
              e.printStackTrace();
          }
      

      【讨论】:

      • 这不适用于获取 Webview 内容,它仅用于获取网页响应。
      【解决方案3】:

      我没有找到任何 webview 函数来获取 HTML 数据。我发现这很有用。

       try {
                  URL twitter = new URL(
                          "http://www.stackoverflow.com");
                  URLConnection tc = twitter.openConnection();
                  BufferedReader in = new BufferedReader(new InputStreamReader(
                          tc.getInputStream()));
      
                  String htmlData = "";
                  String line;
                  while ((line = in.readLine()) != null) {
                         htmlData = htmlData+"\n"+line;
                  }
              } catch (MalformedURLException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
      

      【讨论】:

        猜你喜欢
        • 2013-10-31
        • 1970-01-01
        • 2014-11-13
        • 2014-03-24
        • 1970-01-01
        • 2021-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多