JSON Objects

 JSON Object

JSON stands for JavaScript Object Notation · JSON is a lightweight data-interchange format ·
ak jon manuser unlimited information j vabe HashMap er modde rakhi, JSON Object eu same kaj amra kori JSON Object er moddome.

Example:-

{
        "name": "MD IMRN HASAN",
        "mobile": "017565797**",
        "Email": "imran@gmail.com"
    }


 

JSON Object Request Java Code
XML UI
<Button
android:id="@+id/btnLoad"
android:layout_width="match_parent"
android:layout_height="80dp"
android:textColor="@color/white"
android:textSize="20sp"
android:text="Load Data"
/>


<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="30pt" />

<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardBackgroundColor="#3CFFFFFF"
app:cardCornerRadius="20sp"
android:padding="15dp"
android:visibility="gone"
android:layout_margin="20sp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="12dp"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:padding="5dp"
android:textStyle="bold"
android:text="Name"
android:textColor="@color/white"
/>

<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:padding="5dp"
android:background="#C3112F7A"
android:text=""
android:textColor="@color/white"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:padding="5dp"
android:textStyle="bold"
android:text="Mobile"
android:textColor="@color/white"
/>

<TextView
android:id="@+id/tvMobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:padding="5dp"
android:background="#C3112F7A"
android:text=""
android:textColor="@color/white"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:padding="5dp"
android:textStyle="bold"
android:text="Email"
android:textColor="@color/white"
/>

<TextView
android:id="@+id/tvEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:padding="5dp"
android:background="#C3112F7A"
android:text=""
android:textColor="@color/white"
/>



</LinearLayout>
</androidx.cardview.widget.CardView>


Internet Permission In Manifest 
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

Initialize XML Components 
tvName = findViewById(R.id.tvName);
tvMobile = findViewById(R.id.tvMobile);
tvEmail = findViewById(R.id.tvEmail);
btnLoad = findViewById(R.id.btnLoad);
cardView = findViewById(R.id.cardView);
progressBar = findViewById(R.id.progressBar);

Button OnClickListener
btnLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

progressBar.setVisibility(View.VISIBLE);
btnLoad.setText("Data Loading..");

StringRequest stringRequest = new StringRequest(Request.Method.GET, "https://thispractice.000webhostapp.com/apps/info.json",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {

progressBar.setVisibility(View.GONE);
btnLoad.setText("Data Loaded");
cardView.setVisibility(View.VISIBLE);

Log.d("response", response);

try {
JSONObject jsonObject = new JSONObject(response);
String name = jsonObject.getString("name");
String mobile = jsonObject.getString("mobile");
String email = jsonObject.getString("email");

tvName.setText(name);
tvMobile.setText(mobile);
tvEmail.setText(email);



} catch (JSONException e) {
throw new RuntimeException(e);
}


}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
}
);

RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);

}
});


 JSON Array

Example:-
[
    {
        "name": "MD IMRN HASAN",
        "mobile": "0175657976",
        "email": "imran1@gmail.com"
    },
    {
        "name": "John Doe",
        "mobile": "1234567890",
        "email": "john.doe@example.com"
    },
    {
        "name": "Alice Smith",
        "mobile": "9876543210",
        "email": "alice.smith@example.com"
    }
]




tvDisplay = findViewById(R.id.tvDisplay);
btnLoad = findViewById(R.id.btnLoad);
progressBar = findViewById(R.id.progressBar);


btnLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

progressBar.setVisibility(View.VISIBLE);
btnLoad.setText("Data Loading..");
String url = "https://thispractice.000webhostapp.com/apps/info.json";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
progressBar.setVisibility(View.GONE);
btnLoad.setText("Data Loaded");


try {
if (response.length() > 0) {
for (int x=0; x<response.length(); x++){
JSONObject jsonObject = response.getJSONObject(0);
String title = jsonObject.getString("title");
String video_url = jsonObject.getString("video_url");
tvDisplay.append(""+title+"\n"+video_url+"\n\n");
}


} else {
// Handle the case where the response array is empty
// You might want to show an error message or handle it as appropriate
Toast.makeText(MainActivity.this, "Don't have a data", Toast.LENGTH_SHORT).show();
Log.e("MainActivity", "Empty response array");
}
} catch (JSONException e) {
// Handle JSON parsing exception more gracefully
e.printStackTrace();
Log.e("MainActivity", "JSON parsing error: " + e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressBar.setVisibility(View.GONE);
btnLoad.setText("Data Load Failed");
// Log the error for debugging purposes
Log.e("MainActivity", "Volley error: " + error.getMessage());
// You might want to show a toast or an alert dialog to inform the user about the error
Toast.makeText(MainActivity.this, "Error loading data", Toast.LENGTH_SHORT).show();
}
});

RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(jsonArrayRequest);



}
});


Volley Library theke

JSONObject ba JSON Array Request er khetre Request Korar somoy Null use kora hoy kintu String Request korar somoy null use kora hoy nah.



JsonArrayRequest jsonArrayRequest = new JsonArrayRequest (Request.Method.GET, url, null, new Response.Listener 

Post a Comment

Post a Comment (0)

Previous Post Next Post