Dean Blakely Dean Blakely - 1 month ago 10
Android Question

How to read an http input stream

The code pasted below was taken from java docs on HttpURLConnection.

I get the following error:

readStream(in)


as there is no such method.

I see this same thing in
the Class Overview for URLConnection at
URLConnection.getInputStream()


Where is
readStream
? The code snippet is provided below:

URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try
{
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in); <-----NO SUCH METHOD
}
finally
{
urlConnection.disconnect();
}

Answer

Try with this code:

InputStream in = address.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
    result.append(line);
}
System.out.println(result.toString());
Comments