এটি শুধুমাত্র হানিকম্ব SDK বা উচ্চতরকে লক্ষ্য করে অ্যাপ্লিকেশনের জন্য নিক্ষেপ করা হয়৷ পূর্ববর্তী SDK সংস্করণগুলিকে লক্ষ্য করে এমন অ্যাপ্লিকেশনগুলিকে তাদের প্রধান ইভেন্ট লুপ থ্রেডগুলিতে নেটওয়ার্কিং করার অনুমতি দেওয়া হয়, তবে এটি অত্যন্ত নিরুৎসাহিত করা হয়৷
এই ব্যতিক্রমটি ঠিক করতে অনুগ্রহ করে নীচের উদাহরণ প্রোগ্রামটি পড়ুন −
এই উদাহরণটি দেখায় কিভাবে আমি android.os.NetworkOnMainThreadException ঠিক করব।
ধাপ 1 − অ্যান্ড্রয়েড স্টুডিওতে একটি নতুন প্রকল্প তৈরি করুন, ফাইল ⇒ নতুন প্রকল্পে যান এবং একটি নতুন প্রকল্প তৈরি করতে সমস্ত প্রয়োজনীয় বিবরণ পূরণ করুন৷
ধাপ 2 − নিম্নলিখিত কোডটি 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>
ধাপ 3 − 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);
}
}
} ধাপ 4 − 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>
আসুন আপনার অ্যাপ্লিকেশন চালানোর চেষ্টা করি৷ আমি ধরে নিচ্ছি আপনি আপনার কম্পিউটারের সাথে আপনার আসল অ্যান্ড্রয়েড মোবাইল ডিভাইসটি সংযুক্ত করেছেন৷ অ্যান্ড্রয়েড স্টুডিও থেকে অ্যাপটি চালাতে, আপনার প্রোজেক্টের অ্যাক্টিভিটি ফাইলগুলির একটি খুলুন এবং টুলবার থেকে রান আইকনে ক্লিক করুন। একটি বিকল্প হিসাবে আপনার মোবাইল ডিভাইস নির্বাচন করুন এবং তারপরে আপনার মোবাইল ডিভাইসটি পরীক্ষা করুন যা আপনার ডিফল্ট স্ক্রীন প্রদর্শন করবে -

