如何在 Android 中加载网页,并将其包装到视图中以便可以以不同的方式查看?

发布于 2025-01-08 21:22:52 字数 226 浏览 4 评论 0原文

我正在编写一个应用程序来查看在线网络文本游戏。我希望能够加载网页,然后从缓冲的网页中获取某些元素并显示它们。

这将需要两个主要组件:

  • 缓冲网页但不显示它的能力,以及
  • 通过特定属性从缓冲网页获取元素的能力

我意识到我的问题有点笼统,但目的足够具体有一个简单的答案。如何缓冲网页而不显示它,以及如何从缓冲的网页中获取某些元素?

谢谢!

I'm writing an app to view an online web text-based game. I want to be able to load the webpage, and then get certain elements from the buffered webpage and display them.

This will require two major components:

  • The ability to buffer a webpage, but not display it, and
  • The ability to get elements from the buffered webpage by a specific property

I realize my question is a little bit general, but the purpose is specific enough to have a simple answer. How do I buffer a webpage without displaying it, and how do I get certain elements from that buffered webpage?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

土豪我们做朋友吧 2025-01-15 21:22:52

只是对你的两个问题的一些想法:

在 Android 中加载网页很容易(使用 WebView),但从它的声音来看,你需要有页面的源来获取特定元素。现在,有两种方法可以解释这一点:您要么想要从网页获取特定数据并将其呈现在本机视图(例如 TextView)中,要么您正在寻找一种方法来操作原始代码并隐藏/显示特定元素。

两者都归结为同一件事:您需要能够以方便的方式访问源并从中获取数据或操作它。因此,您可能需要使用一些 HTML 解析器,例如 JSoup、HTMLCleaner 等(有很多)。这些库通常提供加载本地和远程页面,因此可能不需要先手动获取源代码。

就我对这些库的体验而言,它们基本上都使用某种“树遍历器/步行器”来迭代 HTML 元素的层次结构(文档结构),而且还提供了通过 id、标签查找元素的便捷方法,这样您就可以从特定元素中获取数据,甚至可以操纵原始页面来显示/隐藏您希望用户最终看到的内容。

Just some thoughts on your two questions:

Loading up a webpage is easily enough in Android (using a WebView), but from the sounds of it, you will need to have the page's source to fetch specific elements. Now, there's two ways to interpret this: you either want to grab specific data from a webpage and present that in native views (e.g. TextView), or you're looking for a way to manipulate the original code and hide/show specific elements.

Both boil down to the same thing: you will need to be able to access the source in a convenient way and grab data from it or manipulate it. Hence you'll probably want to use some HTML parser, e.g. JSoup, HTMLCleaner, etc. (there are a whole bunch out there). These libraries normally offer loading both local and remote pages, so there's probably no need to manually go grab the source first.

As far as my experience with these libaries go, they all basically use some sort of a 'tree traverer/walker' to iterate over the hierarchy of HTML elements - the document structure - but also provide convenience methods to find elements by id, tag, name etc. That way you'll be able to grab data from specific elements or even manipulate the original page to show/hide what you want the user to see in the end.

可爱暴击 2025-01-15 21:22:52

您可以使用 HttpURLConnection ,可以解析流:

    HttpURLConnection urlConnection = null;
    URL url = new URL(urlString);

    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setReadTimeout(15000);
    urlConnection.setConnectTimeout(15000);
    urlConnection.setUseCaches(false);
    urlConnection.setDefaultUseCaches(false);

    urlConnection.setRequestMethod("GET");

    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);

    urlConnection.connect();


    inpStream = urlConnection.getInputStream();

You can use HttpURLConnection , can parse the stream:

    HttpURLConnection urlConnection = null;
    URL url = new URL(urlString);

    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setReadTimeout(15000);
    urlConnection.setConnectTimeout(15000);
    urlConnection.setUseCaches(false);
    urlConnection.setDefaultUseCaches(false);

    urlConnection.setRequestMethod("GET");

    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);

    urlConnection.connect();


    inpStream = urlConnection.getInputStream();
翻了热茶 2025-01-15 21:22:52

几种方法从Android 中的 URL,但缺点是 HttpURLConnection 可能会执行您需要的操作而不显示页面。

您可以使用 JSoup 从页面获取元素。您可以在 StackOverflow 中搜索有关它的问题和答案。这是一个这样的例子:Android JSoup 示例

There are a few ways to get data from a URL in Android, but the short of it is that HttpURLConnection will probably do what you need without displaying the page.

You can get elements from the page using JSoup. You can search StackOverflow for questions and answers about it. Here's one such: Android JSoup Example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文