如何从Android中的HTML链接获取页面的HTML源?

我正在开发一个需要从链接获取网页源,然后从该页面解析html的应用程序。

您能给我一些例子,还是从哪里着手开始编写这样的应用程序?


qq_花开花谢_0
浏览 528回答 3
3回答

摇曳的蔷薇

您可以使用HttpClient执行HTTP GET并检索HTML响应,如下所示: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();

30秒到达战场

这个问题有点老了,但是我认为现在已经弃用了,等等DefaultHttpClient,我应该发布我的答案HttpGet。给定URL,此函数应获取并返回HTML。public static String getHtml(String url) throws IOException {    // Build and set timeout values for the request.    URLConnection connection = (new URL(url)).openConnection();    connection.setConnectTimeout(5000);    connection.setReadTimeout(5000);    connection.connect();    // Read and store the result line by line then return the entire string.    InputStream in = connection.getInputStream();    BufferedReader reader = new BufferedReader(new InputStreamReader(in));    StringBuilder html = new StringBuilder();    for (String line; (line = reader.readLine()) != null; ) {        html.append(line);    }    in.close();    return html.toString();}
打开App,查看更多内容
随时随地看视频慕课网APP