Website
{
"name" : "Rakib Shah",
"age" : "22",
"Video" :
[
{
"title" : "Sura Wakhia",
"video_id" : "09tVICW0-zA"
},
{
"title" : "Animal",
"video_id" : "N-EBkrKrK4U"
},
{
"title" : "Sura Yeasin",
"video_id" : "VzJRPQDzB7I"
}
]
}dependenciesimplementation libs.volley
implementation 'com.squareup.picasso:picasso:2.8'XML Main Activity<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#FFFFFF"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_margin="10dp"
>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
</LinearLayout>Item. XML<?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:layout_margin="10dp"
>
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title Here"
android:textSize="25sp"
android:textStyle="bold"
/>
<ImageView
android:id="@+id/imageThumb"
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="@drawable/img"
android:scaleType="centerCrop"
/>
</LinearLayout>JAVApackage com.rakibshah.server;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
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.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
TextView textView;
ProgressBar progressBar;
ListView listView;
//ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
//HashMap <String, String> hashMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
listView = findViewById(R.id.listView);
textView = findViewById(R.id.textView);
String url = "https://asoshikhi.com/Drive/complex.json";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject objectResponse) {
progressBar.setVisibility(View.GONE);
Log.d("serverResponse", objectResponse.toString());
try {
String name = objectResponse.getString("name");
String age = objectResponse.getString("age");
textView.append(name);
textView.append("\n");
textView.append(age);
JSONArray jsonArray = objectResponse.getJSONArray("videos");
JSONObject jsonObject = jsonArray.getJSONObject(0);
String title = jsonObject.getString("title");
String video_id = jsonObject.getString("video_id");
textView.append(title);
textView.append("\n");
textView.append(video_id);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(jsonObjectRequest);
}
}JAVA (ChatGPT Code)package com.rakibshah.server;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
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.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private ProgressBar progressBar;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize UI elements
progressBar = findViewById(R.id.progressBar);
listView = findViewById(R.id.listView);
textView = findViewById(R.id.textView);
// URL of the JSON file
String url = "https://asoshikhi.com/Drive/complex.json";
// Create a JsonObjectRequest
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// Hide the progress bar when the response is received
progressBar.setVisibility(View.GONE);
Log.d("serverResponse", response.toString());
try {
// Extract data from the JSON response
String name = response.getString("name");
String age = response.getString("age");
// Update the TextView with the data
textView.append(name + "\n");
textView.append(age + "\n");
// Extract videos array from the response
JSONArray videosArray = response.getJSONArray("videos");
for (int i = 0; i < videosArray.length(); i++) {
JSONObject videoObject = videosArray.getJSONObject(i);
String title = videoObject.getString("title");
String video_id = videoObject.getString("video_id");
// Update the TextView with the video details
textView.append("Title: " + title + "\n");
textView.append("Video ID: " + video_id + "\n");
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Error parsing JSON", Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Hide the progress bar when an error occurs
progressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "Error fetching data: " + error.getMessage(), Toast.LENGTH_SHORT).show();
Log.e("serverError", error.toString());
}
}
);
// Add the request to the RequestQueue
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjectRequest);
}
}
No comments:
Post a Comment