What is the preferred way to read/write content from a URL in Java
What is the preferred way to open a connection to a website and then subsequently read the information on that page? There seem to be many specific questions a开发者_开发知识库bout different parts, but no clear and simple examples.
Getting Text from a URL | Example Depot:
try {
// Create a URL for the desired page
URL url = new URL("http://hostname:80/index.html");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
When it comes to "writing" to a URL I suppose you would want something like Sending a POST Request Using a URL | Example Depot.
You can simply use this:
InputStream stream = new URL( "http://google.com" ).openStream();
Sun Microsystems actually has a tutorial on reading and writing with a URLConnection on this topic that would be a good starting place.
The Sun Microsystems article linked to by SB would be a good place to start. However, I definitely wouldn't call it the preferred way. First it unnecessarily throws Exception, then it doesn't close the streams in a finally. Additionally, it uses the url.openStream method which I disagree with as it may still receive output even if a HTTP error is returned.
Instead of url.openStream
we write:
HttpURLConnection conn=(HttpURLConnection) url.openConnection()
//Any response code starting with 2 is acceptable
if(!String.valueOf(conn.getResponseCode()).startsWith('2'))
//Provide a nice useful exception
throw new IOException("Incorrect response code "+conn.getResponseCode()+" Message: " +getResponseMessage());
InputStream rawIn=conn.getInputStream()
OutputStream rawOut=conn.getOutputStream()
//You may want to add buffering to reduce the number of packets sent
BufferedInputStream bufIn=new BufferedInputStream(rawIn);
BufferedOutputStream bufOut=new BufferedInputStream(rawOut);
DO NOT USE THIS CODE WITHOUT HANDLING EXCEPTIONS OR CLOSING THE STREAMS!. This is actually quite hard to do correctly. If you want to see how to do this properly, please look at my answer to the much more specific question of fetching images in Android as I don't want to rewrite it all here.
Now, when retrieving input from the server, unless you are writing a command line tool, you'll want to have run this in a separate thread and display a loading dialogue. Sgarman's answer demonstrates this using basic Java threads, while my an answer to that question using the AsyncTask class from Android to make it neater. The class file doesn't have any Android dependencies and the license is Apache, so you can use it in non-Android projects.
精彩评论