Showing posts with label Class 255. Show all posts
Showing posts with label Class 255. Show all posts

JSON Array Request 255

 JSON Server Code

[

{

"title" :"Rakib Shah",

"video_id" : "xhUPXIFiAKk"

},

{

"title" :"Abid Shah",

"video_id" : "rrj9yiHrKc4"

},

{

"title" :"Prova Akter",

"video_id" : "e47QmqVMhGc"

},

{

"title" :"Abid Shah",

"video_id" : "rrj9yiHrKc4"

},

{

"title" :"Prova Akter",

"video_id" : "e47QmqVMhGc"

}

]


Build Gradle

implementation 'com.android.volley:volley:1.2.1'

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

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_margin="20dp"
>


<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="10sp"
/>

</LinearLayout>

JAVA

package com.rakibshah.drivinglicense;

import android.os.Bundle;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

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.JsonArrayRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

TextView textView;

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

textView = findViewById(R.id.textView);

String url = "https://asoshikhi.com/Drive/job.json";

JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {

try {


for(int x=0; x<response.length(); x++){

JSONObject jsonObject = response.getJSONObject(x);
String title = jsonObject.getString("title");
String video_id = jsonObject.getString("video_id");
textView.append(x+"."+title+"\n"+video_id+"\n\n");

}

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

}
}, new Response.ErrorListener() {
@Override

public void onErrorResponse(VolleyError error) {

textView.setText(""+error.getMessage());

}
});

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

}
}