1

I'm trying to get ip address of machine with host name nirranjan-pc in android and tried using below code as per the answer of this question

Log.e(tag, "In background method :: ");
try {
    String ip = InetAddress.getByName("nirranjan-pc").getHostAddress();
    Log.e(tag, "++++++++++++ IP FOUND :: " + ip);
} catch (Exception ex) {
    Log.e(tag, "Exception:: " + ex);
    Log.e(tag, Log.getStackTraceString(ex));
    Log.e(tag, "Exception Host ", ex);
} finally {
    Log.e(tag, "Finally::");
}

but i was not getting the ip address and unable to get full stack trace of UnknownHostException using Log.e(tag, Log.getStackTraceString(ex)); and Log.e(tag, "Exception Host ", ex); methods.

Only Log.e(tag, "Exception:: " + ex); method works fine.

my logcat is below.

02-23 12:23:24.308 16507-16618/in.gauriinfotech.sevadeal E/Favour: In background method :: 
02-23 12:23:24.350 16507-16513/in.gauriinfotech.sevadeal W/art: Suspending all threads took: 14.457ms
02-23 12:23:24.377 16507-16618/in.gauriinfotech.sevadeal E/Favour: Exception:: java.net.UnknownHostException: Unable to resolve host "nirranjan-pc": No address associated with hostname
02-23 12:23:24.380 16507-16618/in.gauriinfotech.sevadeal E/Favour: Exception Host 
02-23 12:23:24.380 16507-16618/in.gauriinfotech.sevadeal E/Favour: Finally::

My Question

1) Why Log.e(tag, Log.getStackTraceString(ex)); and Log.e(tag, "Exception Host ", ex); methods not functioning properly and no giving stack trace.

2) I'm getting correct IP address without any error in java desktop application. Why it is not working in android?

-- Also i tried to access same using Google Chrome browser in android

It works fine with IP address

When i try with IP address

But not working with Machine Name

When i try with Machine Name

AND strange is that, when i try to open website from browser from Windows Phone, it opens with no error.

Community
  • 1
  • 1
ELITE
  • 5,815
  • 3
  • 19
  • 29

2 Answers2

2

The reason of your stacktrace is in the source of the Log class.
The UnknownHostException exceptions are not printed.

If you check the code you can see that Log.getStackTraceString() method has been patched in ICS to return in empty String in case of UnknownHostException.

Here the relevant code:

//To reduce the amount of log spew that apps do in the non-error
// condition of the network being unavailable.

Throwable t = tr;
while (t != null) {
    if (t instanceof UnknownHostException) {
        return "";
    }
 t = t.getCause();
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
1

Regarding your first question, try Throwable.printStackTrace(PrintWriter pw) to get the stack trace like:

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
Log.e(tag, sw.toString());

For your second question it is probably your DNS is not configured correctly to lookup and translate a hostname to a correct IP.

EDIT:

Take a look at this for your second question as well Android Browser hostnames does not get resolved if domain name is not appended

Community
  • 1
  • 1
Pooya
  • 6,083
  • 3
  • 23
  • 43
  • i checked the question and answer.. it means there is no way to open connection using machine name instead of IP address.?? – ELITE Feb 23 '16 at 09:57
  • @ELITE the use of machine name was generally used by NetBIOS or Zeroconf protocol which apparently is not implemented in android. you can use a simple dns server on your machine if you want to use name instead of IP – Pooya Feb 23 '16 at 09:59