volley and picasso Libery
implementation 'com.android.volley:volley:1.2.1'
implementation 'com.squareup.picasso:picasso:2.8'
ArrayList and JSONArray - XML COD
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="#E4EEED"
>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="@+id/ListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
ArrayList And JSONArray - JAVA CODE
package com.mahfuj.serverapi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
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.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.squareup.picasso.Picasso;
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 {
ProgressBar progressBar;
ListView ListView;
HashMap<String, String> hashMap;
ArrayList< HashMap<String, String> > arrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
ListView = findViewById(R.id.ListView);
String Url = "https://arahlab.000webhostapp.com/apps/info.json";
JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET, Url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
progressBar.setVisibility(View.GONE);
try {
for (int x=0; x< response.length(); x++){
JSONObject jsonObject = response.getJSONObject(x);
String titel = jsonObject.getString("titel");
String video_link = jsonObject.getString("video_link");
hashMap = new HashMap<>();
hashMap.put("titel", titel);
hashMap.put("video_link", video_link);
arrayList.add(hashMap);
}
MyAdaptar myAdaptar = new MyAdaptar();
ListView.setAdapter(myAdaptar);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "Server Problems", Toast.LENGTH_SHORT).show();
}
});
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
queue.add(arrayRequest);
}
private class MyAdaptar extends BaseAdapter{
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = getLayoutInflater();
View myview = layoutInflater.inflate(R.layout.item, null);
TextView Textview = myview.findViewById(R.id.Textview);
ImageView imageview = myview.findViewById(R.id.imageview);
HashMap<String, String> hashMap = arrayList.get(position);
String titel = hashMap.get("titel");
String video_link = hashMap.get("video_link");
String image_url = "https://img.youtube.com/vi/"+video_link+"/0.jpg";
Textview.setText(titel);
Picasso.get().load(image_url).placeholder(R.drawable.imager).into(imageview);
return myview;
}
}
}