Understanding HttpURLConnection, OutputStream, InputStream, and Buffered Writers requests for MySQL database usage

2

I'm developing an android application that will have access to a MySQL database using webservice . I saw some tutorials on the net and an English tutorial able to connect and insert data, thus creating a login and registration system. But I could not understand how and what is the code HttpURLConnection , OutputStream , InputStream and Buffered Writer used in the tutorial that made the bug run.

I would like an explanation of each one of them and how all together they could make the business work ...

    
asked by anonymous 23.06.2016 / 14:49

1 answer

1

It's a complex question, but I'll try to summarize what each one does and what it does according to the documentation.

1 - HttpURLConnection

This is an abstract class that contains the methods used to make a HTTP connection. It extends the class URLConnection .

Usage example:

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 of type HttpURLConnection, we have to instantiate the URL class. If you check the documentation, the URL class represents "Uniform Resource Locator," a note to a Web site.

After the URL class is instantiated, it is the HttpURLConnection class that will be responsible for connecting to this named place.

2 - OutputStream

Used when you need to write data to somewhere, be it a file, network connections, etc. In this case, it is used to write the data in a HTTP connection.

Usage example:

// 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 is 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 .

Usage example:

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 BufferedReader class, so it is used to read data from InputStream .

That said, you can already pull out what the class BufferedWriter would serve, would the opposite of BufferedReader .

Aside from the android documentation, you can take a look at this community link in English.

    
23.06.2016 / 16:12