Skip to content

Instantly share code, notes, and snippets.

@ginpei
Created February 10, 2017 00:05
Show Gist options
  • Save ginpei/8ee4fc0486bb8b9c3a55e6f428f065bd to your computer and use it in GitHub Desktop.
Save ginpei/8ee4fc0486bb8b9c3a55e6f428f065bd to your computer and use it in GitHub Desktop.
// fetch
URL url = new URL("https://www.wikipedia.org/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in;
try {
in = new BufferedInputStream(connection.getInputStream());
} finally {
connection.disconnect();
}
// read
final int cbufLength = 128;
char[] cbuf = new char[cbufLength];
InputStreamReader reader = new InputStreamReader(in);
try {
for (int i = 0; true; i++) {
int result = reader.read(cbuf, 0, cbufLength);
Log.d(TAG, "@" + (i * cbufLength) + " Read: " + String.valueOf(cbuf));
if (result < 0) {
break;
}
}
} catch (IOException e) {
Log.d(TAG, "IOException at somewhere!");
e.printStackTrace();
}
D/G#Stop: @0 Read: <!DOCTYPE html>
<html lang="mul" class="no-js">
<head>
<meta charset="utf-8">
<title>Wikipedia</title>
<meta name="description"
D/G#Stop: @128 Read: content="Wikipedia is a free online encyclopedia, created and edited by volunteers around the world and hosted by the Wikimedia
D/G#Stop: @256 Read: Fontent="Wikipedia is a free online encyclopedia, created and edited by volunteers around the world and hosted by the Wikimedia
D/G#Stop: IOException at somewhere!
W/System.err: java.net.SocketException: Socket is closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment