OK so I redefined my last program… here it is:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class asp {
public static void main(String[] args) {
try {
URL game = new URL("http://localhost/mystikrpg/post.php?players");
URLConnection connection = game.openConnection();
BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
The problem? When I run it… I get the WHOLE page… EVEN THE CODE SOURCE such as the beginning of the html tag all the way to the end of the body and html tag.
When really… I want it to output is the 1….
The only way I can see it is if I split the string from <body> and </body>…
Meh. Help?
Well, that’s basically what an HTML page is; so that’s what you get. Now, if you don’t want to parse the content manually, use an HTML Parser. There are many of them but I would recommend Jsoup, one of the most elegant available library (clean and nice API, jQuery like CSS selectors, non-verbose element iteration, etc). Demo:
Look Ma, no hands!
PS: As a side note, I must say that I agree with some other answers here, you should maybe consider producing something else than HTML like XML, JSON or even raw text (at least as an alternative to the HTML version if you really need it).