In picking the right network function implementation and higher-level functions, it's crucial to first determine your requirements. As we saw before, it's highly important to know what our needs are in order to avoid common pitfalls.
For the most basic case of merely submitting data to a server and querying the same server for updated data, not much is required beyond the basics of submitting and receiving data via a network socket. Here, we can either use a higher level (for example HTTP) API or plain sockets.
On Android, we can use the Java networking API (java.net
) after setting the Internet permission in the application manifest, for example, using the standard URLConnection
API:
URL url = new URL("http://www.example.com"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter( connection.getOutputStream()); out.write("Hello WOrld!"); out.close(); BufferedReader in = new BufferedReader(new InputStreamReader...