Read file from URL

Read file from URL to array of byte and convert to UTF-8 String Android examle source code.

URL urlLoc = new URL("http://myweb.com/myfile.html");
URLConnection conexion = urlLoc.openConnection();
conexion.setConnectTimeout(4000);
conexion.setReadTimeout(1000);
conexion.connect();
 
// downlod the file
InputStream input = new BufferedInputStream(urlLoc
		.openStream());
 
StringBuffer responseBuffer = new StringBuffer();
byte[] byteArray = new byte[1024];
while (input.read(byteArray) != -1)
 {   
        String res = new String(byteArray, "UTF-8");
        responseBuffer.append(res);
        byteArray = null;
        byteArray = new byte[1024];
 }
 
String response = responseBuffer.toString().trim();
This entry was posted in Android Examples Code and tagged , , , , , . Bookmark the permalink.


Comments are closed.



6,474 views