Post

Class 253- JSON অবজেক্ট পার্স করা এবং ডেটা এক্সট্র্যাক্ট করা

Class 253- JSON অবজেক্ট পার্স করা এবং ডেটা এক্সট্র্যাক্ট করা

এই ক্লাসে আমরা Android অ্যাপে JSON ডেটা পার্স করার পদ্ধতি শিখব। আমরা Volley লাইব্রেরি ব্যবহার করে নেটওয়ার্ক থেকে JSON ডেটা ফেচ করব এবং সেখান থেকে নাম, মোবাইল, ইমেইল, ঠিকানা ইত্যাদি ডেটা এক্সট্র্যাক্ট করব।


Volley লাইব্রেরি ব্যবহার করে JSON পার্সিং

Volley কি?
Volley হল Android-এর একটি HTTP লাইব্রেরি, যা নেটওয়ার্ক রিকোয়েস্ট এবং রেসপন্স হ্যান্ডেলিং সহজ করে। এটি JSON ডেটা পার্স করার জন্য খুবই জনপ্রিয়।


স্টেপ ১: প্রোজেক্টে Volley যোগ করা

আপনার build.gradle (Module: app) ফাইলে Volley ডিপেন্ডেন্সি যোগ করুন:

1
2
3
dependencies {
    implementation 'com.android.volley:volley:1.2.1'
}

সিঙ্ক করুন এবং প্রোজেক্ট রিবিল্ড করুন।


স্টেপ ২: ইন্টারনেট পারমিশন যোগ করা

আপনার AndroidManifest.xml ফাইলে ইন্টারনেট পারমিশন যোগ করুন:

1
<uses-permission android:name="android.permission.INTERNET" />

স্টেপ ৩: JSON ডেটা ফেচ এবং পার্স করার কোড

নিচের উদাহরণে আমরা একটি JSON অবজেক্ট ফেচ করব এবং সেখান থেকে নাম, মোবাইল, ইমেইল, ঠিকানা ইত্যাদি ডেটা এক্সট্র্যাক্ট করব।

JSON উদাহরণ:

1
2
3
4
5
6
{
  "name": "আবুল",
  "mobile": "01234567890",
  "email": "abul@example.com",
  "address": "ঢাকা, বাংলাদেশ"
}

MainActivity.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.example.jsonparsing;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    private TextView nameTextView, mobileTextView, emailTextView, addressTextView;
    private RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // TextView ইনিশিয়ালাইজ করা
        nameTextView = findViewById(R.id.nameTextView);
        mobileTextView = findViewById(R.id.mobileTextView);
        emailTextView = findViewById(R.id.emailTextView);
        addressTextView = findViewById(R.id.addressTextView);

        // Volley রিকোয়েস্ট কিউ তৈরি করা
        requestQueue = Volley.newRequestQueue(this);

        // JSON ডেটা ফেচ করার URL
        String url = "https://example.com/api/user"; // আপনার API URL এখানে বসান

        // JSON রিকোয়েস্ট তৈরি করা
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            // JSON থেকে ডেটা এক্সট্র্যাক্ট করা
                            String name = response.getString("name");
                            String mobile = response.getString("mobile");
                            String email = response.getString("email");
                            String address = response.getString("address");

                            // TextView-এ ডেটা সেট করা
                            nameTextView.setText("নাম: " + name);
                            mobileTextView.setText("মোবাইল: " + mobile);
                            emailTextView.setText("ইমেইল: " + email);
                            addressTextView.setText("ঠিকানা: " + address);

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Log.e("JSON Parsing Error", e.getMessage());
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Volley Error", error.toString());
                    }
                }
        );

        // রিকোয়েস্ট কিউ-তে রিকোয়েস্ট যোগ করা
        requestQueue.add(jsonObjectRequest);
    }
}

স্টেপ ৪: লেআউট ফাইল তৈরি করা

activity_main.xml ফাইলে নিচের কোড যোগ করুন:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/nameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="নাম: "
        android:textSize="18sp"
        android:layout_marginBottom="8dp"/>

    <TextView
        android:id="@+id/mobileTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="মোবাইল: "
        android:textSize="18sp"
        android:layout_marginBottom="8dp"/>

    <TextView
        android:id="@+id/emailTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ইমেইল: "
        android:textSize="18sp"
        android:layout_marginBottom="8dp"/>

    <TextView
        android:id="@+id/addressTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ঠিকানা: "
        android:textSize="18sp"/>
</LinearLayout>

স্টেপ ৫: অ্যাপ রান করা

  1. আপনার API URL সঠিকভাবে সেট করুন।
  2. অ্যাপ রান করুন এবং JSON ডেটা ফেচ করে UI-তে দেখান।

সারমর্ম

  • Volley ব্যবহার করে Android অ্যাপে JSON ডেটা ফেচ করা খুবই সহজ।
  • JsonObjectRequest দিয়ে JSON অবজেক্ট পার্স করা যায়।
  • JSON থেকে ডেটা এক্সট্র্যাক্ট করে UI-তে দেখানো যায়।

এই ক্লাসে আমরা Android-এ JSON পার্সিং শিখলাম। পরবর্তী ক্লাসে আমরা JSON অ্যারে পার্সিং শিখব। 😊