Skip to main content

networking

In Java Android, a network refers to the communication between devices over the internet or a local network. It involves sending and receiving data between a client (such as an Android device) and a server (a remote computer or service). Network operations are essential for tasks like fetching data from a web service, downloading files, sending emails, or establishing real-time communication.

There are several components and concepts involved in network programming in Java Android:

  1. URL: The URL class represents a Uniform Resource Locator, which is the address of a resource on the internet. It provides methods to retrieve information from a given URL, such as opening a connection and reading the data.

  2. HttpURLConnection: The HttpURLConnection class is a subclass of the URLConnection class and provides an HTTP-specific implementation. It allows you to open a connection, set properties (like request method, timeouts, headers), and read/write data.

Example:

try {
URL url = new URL("https://example.com/data");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// Set connection properties, such as request method, timeouts, headers, etc.
connection.setRequestMethod("GET");

// Read the response
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}

// Close resources
reader.close();
connection.disconnect();

// Process the response
String responseData = response.toString();
// ... do something with the data
} catch (IOException e) {
e.printStackTrace();
}
  1. AsyncTask or Thread: Network operations should not be performed on the main UI thread, as they may cause the application to become unresponsive. You can use either AsyncTask or Thread to perform network operations in a background thread and update the UI thread when necessary.

Example using AsyncTask:

public class Network {
public static final String TAG = "Network";

public void getDataFromUrl(String stringUrl) {
new NetworkTask().execute(stringUrl);
}

private class NetworkTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String stringUrl = params[0];
try {
URL url = new URL(stringUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// Set connection properties, such as request method, timeouts, headers, etc.
connection.setRequestMethod("GET");

// Read the response
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}

// Close resources
reader.close();
connection.disconnect();

// Process the response
return response.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

@Override
protected void onPostExecute(String responseData) {
if (responseData != null) {
Log.d(TAG, "getDataFromUrl: " + responseData);
// ... do something with the data
} else {
Log.d(TAG, "Failed to fetch data");
}
}
}
}

MainActivity.java

    Network network = new Network();
network.getDataFromUrl("https://jsonplaceholder.typicode.com/todos/1");
  1. Callbacks and listeners: Asynchronous network operations often use callbacks or listeners to handle the result or notify the completion of a task. For example, when making a network request using a library like Volley or Retrofit, you define callback interfaces to handle success or failure scenarios.

Example using a callback interface:

NetworkCallback.java

public interface NetworkCallback {
void onSuccess(String result);
void onError(Exception e);
}

NetworkCallBackExample.java

public class NetworkCallBackExample {
@SuppressLint("StaticFieldLeak")
public void fetchDataFromServer(NetworkCallback callback, String stringUrl) {
new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// ...

// Read the response
StringBuilder response = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}

// Close resources
reader.close();
connection.disconnect();

// Return the response
return response.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

@Override
protected void onPostExecute(String responseData) {
if (responseData != null) {
callback.onSuccess(responseData);
} else {
callback.onError(new Exception("Failed to fetch data from the server."));
}
}
}.execute(stringUrl);
}
}

MainActivity.java

networkCallBackExample.fetchDataFromServer(new NetworkCallback() {
@Override
public void onSuccess(String result) {
Log.d(TAG, "onSuccess: "+result);
}

@Override
public void onError(Exception e) {

}
},"https://jsonplaceholder.typicode.com/todos/1");