Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read an HTTP input stream?

Tags:

java

android

The code pasted below was taken from Javadocs 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();        }  
like image 204
Dean Blakely Avatar asked Mar 24 '12 22:03

Dean Blakely


People also ask

How do you read input stream data?

InputStream. read() method reads the next byte of the data from the the input stream and returns int in the range of 0 to 255. If no byte is available because the end of the stream has been reached, the returned value is -1.

How do you create an input stream object to read a file?

Java FileInputStream constructorsFileInputStream(File file) — creates a file input stream to read from a File object. FileInputStream(String name) — creates a file input stream to read from the specified file name. FileInputStream(FileDescriptor fdObj) — creates a file input read from the specified file descriptor.

What is an input stream?

InputStream , represents an ordered stream of bytes. In other words, you can read data from a Java InputStream as an ordered sequence of bytes. This is useful when reading data from a file, or received over the network.


1 Answers

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()); 
like image 162
Emran Avatar answered Sep 23 '22 20:09

Emran