It is a complex question, but I will try to summarize what each one does and what it serves according to the documentation.
1 - HttpURLConnection
This is an abstract class that contains the methods used to make a connection HTTP
. She extends the class URLConnection
.
Example of use:
URL url = new URL("http://teste.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
See for example that to get an instance like Httpurlconnection, we have to instantiate the URL class. If you check the documentation, the URL class represents "Uniform Resource Locator", a pointer to a Web place.
After instantiating the URL class, it is the Httpurlconnection class that will be responsible for making the connection to this pointed place.
2 - OutputStream
It is used when you need to write (write) data somewhere, be it a file, network connections, etc. In this case, it is used to write the data on a connection HTTP
.
Example of use:
// Antes de você escrever os dados, você precisa dizer a conexão que você vai fazer isso
conn.setDoOutput(true);
// Instancia o OutputStreamWriter usando o OutputStream da conexão que você efetuou
OutputStreamWriter writer = new OutputStreamWriter(
conn.getOutputStream());
// Escreve os dados para a conexão.
// Nenhum dado é escrito até que você utilize este método.
writer.write("message=" + message);
3 - InputStream
\ BufferedReader
It’s the opposite of OutputStream
, it serves to read(read) the data from somewhere. In this case it is used to establish a read contract with the connection HTTP
.
Example of use:
BufferedReader reader = new BufferedReader(conn.getInputStream());
StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
Following the example above, it reads the response in a string using the class BufferedReader
, thus, it serves to read the data coming from InputStream
.
That said, you can already draw for what the class is for BufferedWriter
, would be the opposite of BufferedReader
.
Out of the android documentation, you can take a look at this link community in English.
Dear, it depends on study. No doubt (do not misunderstand me ok). Not understanding the whole layer of web communication is because you don’t know what objects do. I believe that here in the OS your doubt will be well seen if it is punctual. For example: Because Httpurlconnection has a timeout method. If your Android documentation lacks information, go to the Oracle documentation and study what Streams, Bufferedwriter, Reader etc are.
– Mateus
Thank you professor... I am a computer technician and I studied web html programming, css and php. I haven’t really gone into the world of object orientation. I took some basic java to be able to understand the android programming that is my passion and I intend to create APP for the community of my region. Thank you.
– mahenrocha