কম্পিউটার টিউটোরিয়াল

অ্যান্ড্রয়েডে GSON ব্যবহার করে HashMap কনভার্ট করে JSON-এ রূপান্তর করার পদ্ধতি

GSON লাইব্রেরি পরিচিতি

GSON হল একটি জাভা লাইব্রেরি, যা অবজেক্টকে JSON-এ এবং JSON কে অবজেক্টে রূপান্তর করতে ব্যবহৃত হয়। এটি ইন্টারন্যালি সিরিয়ালাইজেশন (serialization) এবং ডিসিরিয়ালাইজেশন (deserialization) এর উপর ভিত্তি করে কাজ করে।

উদাহরণ: HashMap কে JSON-এ রূপান্তর

এই উদাহরণটি দেখাবে কীভাবে GSON লাইব্রেরি ব্যবহার করে HashMap কে JSON-এ রূপান্তর করতে হয়।

স্টেপ ১: নতুন প্রজেক্ট তৈরি

Android Studio-তে File → New Project এ যান এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।

স্টেপ ২: build.gradle-এ ডিপেন্ডেন্সি যোগ করুন

apply plugin: 'com.android.application'
android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.andy.myapplication"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

উপরের কোডে GSON-এর সর্বশেষ লাইব্রেরি (`com.google.code.gson:gson:2.8.5`) যোগ করা হয়েছে।

স্টেপ ৩: লেআউট XML কোড (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ফলাফল ডেটা"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

উপরের কোডে ফলাফল দেখানোর জন্য একটি TextView যোগ করা হয়েছে।

স্টেপ ৪: MainActivity.java কোড

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView result = findViewById(R.id.result);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("JAVA", "NetBeans");
        hashMap.put("Android", "Android Studio");
        hashMap.put("Kotlin", "Notepad ++");
        Gson gson = new Gson();
        String mapData = gson.toJson(hashMap);
        result.setText(mapData);
    }
}

অ্যাপ্লিকেশন চালানোর পদ্ধতি

আপনার অ্যান্ড্রয়েড মোবাইল ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে ধরে নেওয়া হয়েছে। Android Studio-তে প্রজেক্টের এক্টিভিটি ফাইল খুলে টুলবারের Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস সিলেক্ট করুন এবং ডিভাইস চেক করুন - ডিফল্ট স্ক্রিন প্রদর্শিত হবে।

অ্যান্ড্রয়েডে GSON ব্যবহার করে HashMap কনভার্ট করে JSON-এ রূপান্তর করার পদ্ধতি

উপরের আউটপুটে HashMap JSON ডেটা হিসেবে প্রদর্শিত হচ্ছে।

  1. অ্যান্ড্রয়েডে JSON পার্স করার সহজ পদ্ধতি

  2. অ্যান্ড্রয়েডে Bitmap কে Drawable-এ রূপান্তর করা

  3. অ্যান্ড্রয়েডে JSON অ্যারে ইটারেট করার পদ্ধতি

  4. অ্যান্ড্রয়েডে OCR ব্যবহার করে ছবি থেকে টেক্সট কনভার্ট করার পদ্ধতি