2017-02-09 6 views
0

Ich implementiere eine Methode, die den Hostnamen von Android-Gerät erhält. Ich verwende die InetAddress-Klasse dafür. Allerdings bekomme ich eine fatale Ausnahme. Hier ist die Methode. Ich habe einen try/catch-Block hinzugefügt, funktioniert aber immer noch nicht. Jede Hilfe wird geschätzt. Herausgegeben und hinzugefügt AusnahmefehlerWie bekomme ich den Hostnamen von Android-Gerät

/** 
    * 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; 
    } 

schwerwiegende Ausnahme

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)  
+0

"Ich bekomme eine schwerwiegende Ausnahme" - Bitte bearbeiten Sie Ihre Frage und posten ** den gesamten Java-Stack-Trace **, der dieser Ausnahme zugeordnet ist. – CommonsWare

Antwort

0
Caused by: android.os.NetworkOnMainThreadException 

anderen Thread verwenden, um Vernetzung zu tun. AsyncTask kann Ihnen helfen.

+0

Ich benutze auch WifiManager-Klasse, aber das hat nicht über die Arbeit am Haupt-Thread beschwert. Soll ich auch einen eigenen Thread für diesen erstellen? Danke ich kenne separate Frage – miatech

Verwandte Themen