URL to String
Apache Commons IO is a java library with simple IO utilities and filters.
This requires the library commons-io-2.1.jar to be in classpath.
The following example shows using IOUtils.toString() API.
package com.bethecoder.tutorials.commons_io.tests;
import java.io.IOException;
import java.net.URL;
import org.apache.commons.io.IOUtils;
public class URLToString {
/**
* @param args
* @throws IOException
*/
public static void main ( String [] args ) throws IOException {
URL url = new URL ( "http://finance.google.com/finance/info?client=ig&q=NASDAQ:MSFT" ) ;
String content = IOUtils.toString ( url ) ;
System.out.println ( content ) ;
}
}
It gives the following output,
// [
{
"id": "358464"
,"t" : "MSFT"
,"e" : "NASDAQ"
,"l" : "24.79"
,"l_cur" : "24.79"
,"s": "1"
,"ltt":"4:00PM EST"
,"lt" : "Nov 22, 4:00PM EST"
,"c" : "-0.21"
,"cp" : "-0.84"
,"ccol" : "chr"
,"el": "24.52"
,"el_cur": "24.52"
,"elt" : "Nov 23, 4:25AM EST"
,"ec" : "-0.27"
,"ecp" : "-1.09"
,"eccol" : "chr"
,"div" : "0.20"
,"yld" : "3.23"
}
]