【问题标题】:How to force HttpClient to get plain html ?如何强制 HttpClient 获取纯 html ?
【发布时间】:2013-03-22 04:13:13
【问题描述】:

我正在编写简单的 Android 应用程序,它在特定位置解析 html,获取一些数据并显示在 ListView 中。由于应用程序只需要纯 html 响应,因此有可能只请求纯 html。如何从 http 请求中排除 css 和 java 脚本文件、图像和类似文件? 我正在使用 apache HttpClient 对象。

【问题讨论】:

    标签: android httpclient


    【解决方案1】:

    这是否回答了您的问题?如果是这样,请记住在单独的线程上执行此操作以避免 UI 阻塞(可能想使用AsyncTask

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

    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();
    

    【讨论】:

    • 感谢您的回答。我已经实现了(类似的代码)。但我想知道对于特定的 http 请求,它是否只需要普通的 html 响应或所有内容(图像、css 文件、java 脚本文件和类似文件)。如果是,我怎样才能显式禁用除纯 html 响应之外的所有内容...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    相关资源
    最近更新 更多