Android.os.NetworkOnMainThreadException appears suddenly

I have a small application that I made that worked for several months, but suddenly I get

android.os.NetworkOnMainThreadException 

I have not changed the code, this is a line that breaks

 try { endPoint = InetAddress.getByName(target.getToIp()); port = target.getPort(); socket = new DatagramSocket(); } catch(Exception e) { } 

Please note that I am not sending anything, I am just creating instances, it is already split into the InetAddress line.

He visited last night, everything I did on the phone was updated on Google Play, it was updated to the latest version of Google Maps, but this can not have something with it?

It works in an emulator, only my HTC One X that crashes

The full code is here https://github.com/AndersMalmgren/FreePIE/tree/master/Lib/Android/FreePIE%20Android%20IMU

edit: manifest

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.freepie.android.imu" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/title_activity_main" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

I tried to move the problem code to the stream instead, now it will not break, but the socket is set to null.

 running = true; worker = new Thread(new Runnable() { public void run(){ try { endPoint = InetAddress.getByName(target.getToIp()); port = target.getPort(); socket = new DatagramSocket(); } catch(Exception e) { Exception err = e; } while(running) { try { sync.await(); } catch(Exception e) {} Send(); } try { socket.disconnect(); } catch(Exception e) {} } }); worker.start(); 

update: The solution was to add some rejection if the user enters DNS instead of IP https://github.com/AndersMalmgren/FreePIE/commit/e8017e02a7893d9df41e4ed67a037f016b6a7d39

+4
source share
3 answers

You will get a NetworkOnMainThreadException on Android 4.0+ by default if you try to do network I / O in the main thread of the application. getByName() does a DNS lookup if you provide a domain name (instead of notation with IP dots) and NetworkOnMainThreadException .

+5
source

You should put your code in AsyncTask as follows:

 new AsyncTask<Void,Void,Void>(){ doInBackground(){ endPoint = InetAddress.getByName(target.getToIp()); port = target.getPort(); socket = new DatagramSocket(); } }.execute(); 

Please note that this code does not work, please read more about AsyncTasks here

+1
source

Using Network on main thread is not allowed with Android 4.0. If you run the code on Android 2.3 or another, this will not cause any exceptions.

0
source

Source: https://habr.com/ru/post/1493797/


All Articles