【问题标题】:android - how to get html code of a webpage in android?android - 如何在android中获取网页的html代码?
【发布时间】:2012-06-05 19:16:44
【问题描述】:

我想在 android 中获取网页的 html 代码。网页 url 将在编辑文本框中给出,然后当用户单击按钮时,文本视图将显示该网页的代码。请解释并给出代码!

任何帮助将不胜感激!

【问题讨论】:

    标签: android


    【解决方案1】:
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    HttpResponse response = client.execute(request);
    
    String html = "";
    InputStream in = response.getEntity().getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder str = new StringBuilder();
    String line = null;
    while((line = reader.readLine()) != null)
    {
        str.append(line);
    }
    in.close();
    html = str.toString();
    

    别忘了在 AndroidManifest 中添加互联网权限:

    <uses-permission android:name="android.permission.INTERNET" /> 
    

    您可以参考这些链接以获得更多帮助:

    http://lexandera.com/2009/01/extracting-html-from-a-webview/

    Is it possible to get the HTML code from WebView

    How to get the html-source of a page from a html link in android?

    【讨论】:

    • 我如何获得网页的一些 html 源代码?如果我获得完整的源页面,它可能会花费很多时间,而且我不需要所有线路,你能帮我吗?非常感谢
    【解决方案2】:

    您需要HttpClient 来执行HttpGet 请求。然后您可以阅读该请求的内容。

    这个 sn-p 给你一个InputStream

      public static InputStream getInputStreamFromUrl(String url) {
      InputStream content = null;
      try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(new HttpGet(url));
        content = response.getEntity().getContent();
      } catch (Exception e) {
        Log.e("[GET REQUEST]", "Network exception", e);
      }
        return content;
    }
    

    这个方法返回String:

    // Fast Implementation
    private StringBuilder inputStreamToString(InputStream is) {
        String line = "";
        StringBuilder total = new StringBuilder();
    
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    
        // Read response until the end
        while ((line = rd.readLine()) != null) { 
            total.append(line); 
        }
    
        // Return full string
        return total;
    }
    

    来源:http://www.androidsnippets.com/executing-a-http-get-request-with-httpclienthttp://www.androidsnippets.com/get-the-content-from-a-httpresponse-or-any-inputstream-as-a-string

    【讨论】:

    • 你能告诉我如何将 html 代码显示到 textview 中吗?我试过了,没用!莫名其妙停了下来! @基里尔
    • 我假设您已经在布局中定义了一个 TextView。现在您可以使用以下方法访问 TextView:TextView yourTextView = (TextView) findViewById(R.id.NameOfYourTextView); 之后您可以调用方法 yourTextView.setText("The HTML code"); 编辑:还可以查看下面的答案以提取 HTML 代码
    • @Kiril - 我如何获得网页的一些 html 源代码?如果我得到完整的源页面,它可能会花很多时间,而且我不需要所有线路,你能帮我吗?非常感谢
    【解决方案3】:

    使用上面的代码,并将其设置为这样的文本视图:

    InputStream is =InputStream getInputStreamFromUrl("http://google.com");
    String htmlText = inputStreamToString(is);
    
    mTextView.setText(Html.fromHtml(htmlText));
    

    但在单独的线程/异步任务中执行网络请求:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-23
      • 2012-10-13
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多