0

I'm implementing a method that gets the hostname of android device. I'm using InetAddress class for this. however, I'm getting fatal exception. Here's the method. I added a try/catch block, but still not working. Any help appreciated. Edited and added exception error

/**
     * getHostname returns the hostname of android device as string
     *
     * @param context
     * @return hostname
     */

    public String getHostname(Context context) {
        String hostName;
        try {
            InetAddress netHost = InetAddress.getLocalHost();
            hostName = netHost.getHostName();
        } catch (UnknownHostException ex) {
            hostName = null;
        }

        return hostName;
    }

fatal exception

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.george.droidnet, PID: 23439
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.george.droidnet/com.example.george.droidnet.TcpConfigActivity}: android.os.NetworkOnMainThreadException
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                   Caused by: android.os.NetworkOnMainThreadException
                      at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
                      at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
                      at java.net.InetAddress.getLocalHost(InetAddress.java:409)
                      at com.example.george.droidnet.TcpConfigActivity.getHostname(TcpConfigActivity.java:100)
                      at com.example.george.droidnet.TcpConfigActivity.onCreate(TcpConfigActivity.java:40)
                      at android.app.Activity.performCreate(Activity.java:6237)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
miatech
  • 2,150
  • 8
  • 41
  • 78

2 Answers2

1

Man, in Java you can't "do network things" on main thread. To do it, you have to put it into AsyncTask, like this:

public String getHostName() {
    AsyncTack<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
        @Override
        protected Void doInBackground(Void... voids) {
            String hostName;
            try {
                InetAddress netHost = InetAddress.getLocalHost();
                hostName = netHost.getHostName();
            } catch (UnknownHostException ex) {
                hostName = null;
            }

            return hostName;
        }
    };

    task.execute();
    try {
        return task.get();
    } catch (Exception e) {
        return null;
    }
}

This code should work.

0
Caused by: android.os.NetworkOnMainThreadException

Use another thread to do networking. AsyncTask may help you.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • I'm also using WifiManager class, but that didn't complained about working on main thread. Should I create a separate thread for that one too? Thanks I know seperate question – miatech Feb 09 '17 at 20:57