简体   繁体   中英

Unable to get stack trace of `java.net.UnknownHostException` in android

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

当我尝试IP地址

But not working with Machine Name

当我尝试使用机器名称时

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

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM