NetworkOnMainThreadException কেন হয় এবং কিভাবে ফিক্স করবেন
Android Honeycomb (API 11) থেকে মেইন (UI) থ্রেডে নেটওয়ার্ক অপারেশন করার অনুমতি নেই। এটি করার চেষ্টা করলে android.os.NetworkOnMainThreadException থ্রো করে। সমাধান: ব্যাকগ্রাউন্ড থ্রেডে নেটওয়ার্ক কাজ করুন — AsyncTask, Thread, Executor, বা আধুনিকভাবে Coroutines/Retrofit দিয়ে।
ধাপ ১: প্রজেক্ট তৈরি করুন
Android Studio-তে নতুন প্রজেক্ট শুরু করুন।
ধাপ ২: লেআউট (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textStyle="bold|italic" />
<TextView
android:id="@+id/textMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
ধাপ ৩: MainActivity.java - AsyncTask দিয়ে নেটওয়ার্ক কল
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
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 urlString = "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("লোড হচ্ছে...");
new FetchDataTask().execute();
}
private class FetchDataTask extends AsyncTask {
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL(urlString);
BufferedReader reader = new BufferedReader(
new InputStreamReader(url.openStream()));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
reader.close();
return result.toString();
} catch (IOException e) {
e.printStackTrace();
return "ত্রুটি: " + e.getMessage();
}
}
@Override
protected void onPostExecute(String result) {
textMessage.setText(result);
textLoad.setText("সম্পন্ন");
}
}
}
ধাপ ৪: Manifest-এ ইন্টারনেট পারমিশন
<uses-permission android:name="android.permission.INTERNET" />
⚠️ গুরুত্বপূর্ণ নোট
- AsyncTask deprecated Android 11 (API 30) থেকে।
- উৎপাদন-মানের অ্যাপে Retrofit + Coroutines, Volley, বা OkHttp ব্যবহার করুন।
- এই উদাহরণ শুধু কনসেপ্ট বোঝার জন্য।
আধুনিক বিকল্প (Kotlin Coroutines + Retrofit)
// build.gradle
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.0"
// Repository
interface ApiService {
@GET("text.txt")
suspend fun fetchText(): Response
}
// ViewModel / Activity
lifecycleScope.launch {
try {
textLoad.text = "লোড হচ্ছে..."
val response = apiService.fetchText()
textMessage.text = response.body()
textLoad.text = "সম্পন্ন"
} catch (e: Exception) {
textMessage.text = "ত্রুটি: ${e.message}"
}
}
ফলাফল
অ্যাপ চালু হলে "লোড হচ্ছে..." দেখাবে, নেটওয়ার্ক থেকে ডেটা আনবে, তারপর টেক্সট প্রদর্শন করবে। UI ফ্রিজ হবে না।