+
+ // This must run asynchronously because it involves a network request. `String` declares the parameters. `Void` does not declare progress units. `String` contains the results.
+ private static class GetHostIpAddresses extends AsyncTask<String, Void, String> {
+ // The weak references are used to determine if the activity have disappeared while the AsyncTask is running.
+ private final WeakReference<Activity> activityWeakReference;
+
+ GetHostIpAddresses(Activity activity) {
+ // Populate the weak references.
+ activityWeakReference = new WeakReference<>(activity);
+ }
+
+ @Override
+ protected String doInBackground(String... domainName) {
+ // Get handles for the activity and the alert dialog.
+ Activity activity = activityWeakReference.get();
+
+ // Abort if the activity or the dialog is gone.
+ if ((activity == null) || activity.isFinishing()) {
+ // Return an empty spannable string builder.
+ return "";
+ }
+
+ // Initialize an IP address string builder.
+ StringBuilder ipAddresses = new StringBuilder();
+
+ // Get an array with the IP addresses for the host.
+ try {
+ // Get an array with all the IP addresses for the domain.
+ InetAddress[] inetAddressesArray = InetAddress.getAllByName(domainName[0]);
+
+ // Add each IP address to the string builder.
+ for (InetAddress inetAddress : inetAddressesArray) {
+ if (ipAddresses.length() == 0) { // This is the first IP address.
+ // Add the IP address to the string builder.
+ ipAddresses.append(inetAddress.getHostAddress());
+ } else { // This is not the first IP address.
+ // Add a line break to the string builder first.
+ ipAddresses.append("\n");
+
+ // Add the IP address to the string builder.
+ ipAddresses.append(inetAddress.getHostAddress());
+ }
+ }
+ } catch (UnknownHostException exception) {
+ // Do nothing.
+ }
+
+ // Return the string.
+ return ipAddresses.toString();
+ }
+
+ // `onPostExecute()` operates on the UI thread.
+ @Override
+ protected void onPostExecute(String ipAddresses) {
+ // Get handles for the activity and the alert dialog.
+ Activity activity = activityWeakReference.get();
+
+ // Abort if the activity or the alert dialog is gone.
+ if ((activity == null) || activity.isFinishing()) {
+ return;
+ }
+
+ // Store the IP addresses.
+ currentHostIpAddresses = ipAddresses;
+ }
+ }