- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to fix android.os.NetworkOnMainThreadException?
The exception that is thrown when an application attempts to perform a networking operation on its main thread.
This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.
Kindly refer the below example program to fix this exception −
This example demonstrates how do I fix the android.os.NetworkOnMainThreadException.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" android:padding="8dp" tools:context=".MainActivity"> <TextView android:id="@+id/textLoad" android:textSize="12sp" android:textStyle="bold|italic" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/textMessage" android:textSize="16sp" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
Step 3 − Add the following code to src/MainActivity.java
import androidx.appcompat.app.AppCompatActivity; import android.os.AsyncTask; import android.os.Bundle; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; public class MainActivity extends AppCompatActivity { TextView textLoad, textMessage; final String strMessage = "https://sites.google.com/site/androidersite/text.txt"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textLoad = findViewById(R.id.textLoad); textMessage = findViewById(R.id.textMessage); textLoad.setText("Loading..."); new MyTask().execute(); } private class MyTask extends AsyncTask<Void, Void, Void>{ String result; @Override protected Void doInBackground(Void... voids) { URL url; try { url = new URL(strMessage); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream())); String stringBuffer; String string = ""; while ((stringBuffer = bufferedReader.readLine()) != null){ string = String.format("%s%s", string, stringBuffer); } bufferedReader.close(); result = string; } catch (IOException e){ e.printStackTrace(); result = e.toString(); } return null; } @Override protected void onPostExecute(Void aVoid) { textMessage.setText(result); textLoad.setText("Finished"); super.onPostExecute(aVoid); } } }
Step 4 − Add the following code to androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −
Click here to download the project code.