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!

3 Answers3

0

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();
Murat
  • 3,084
  • 37
  • 55
0

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.

Community
  • 1
  • 1
kabuko
  • 36,028
  • 10
  • 80
  • 93
0

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.

MH.
  • 45,303
  • 10
  • 103
  • 116