Understanding Httpurlconnection, Outputstream, Inputstream and Buffered Writers requests for Mysql database use

Asked

Viewed 753 times

2

I am developing an android application that will have access to a database MySQL using webservice. I saw some tutorials on the net and in a tutorial in English able to make the connection and insertion of data, thus creating a login and registration system. But I couldn’t understand how and what the codes are for HttpURLConnection, OutputStream, InputStream and Buffered Writer used in the tutorial and that made the animal rotate.

I’d like an explanation about each of them and how everyone together could make the deal work...

  • 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.

  • 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.

1 answer

1


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.

  • 1

    Got it. It was really clear and explained. Now I know what each code does when it talks to my PHP that connects to Mysql. Although my question is not a 'punctual' question, this answer was very helpful and enlightening. Thank you very much!

  • Thank you friend, if it helped you and clarified your doubts you can select as reply, good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.