Android Volley JSON Kullanımı
ad area Gradle Scripts > build.gradle (Module: app)
dependencies {
...
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.github.bumptech.glide:glide:4.7.1'
...
}
Model > Ogrenci.java
package com.serifgungor.ogrencilerjson.Model;

public class Ogrenci {
    private int ogrenciNo;
    private String ad;
    private String soyad;
    private String bolum;
    private String universite;
    private String resimUrl;

    public Ogrenci() {
    }

    public Ogrenci(int ogrenciNo, String ad, String soyad, String bolum, String universite, String resimUrl) {
        this.ogrenciNo = ogrenciNo;
        this.ad = ad;
        this.soyad = soyad;
        this.bolum = bolum;
        this.universite = universite;
        this.resimUrl = resimUrl;
    }

    public int getOgrenciNo() {
        return ogrenciNo;
    }

    public void setOgrenciNo(int ogrenciNo) {
        this.ogrenciNo = ogrenciNo;
    }

    public String getAd() {
        return ad;
    }

    public void setAd(String ad) {
        this.ad = ad;
    }

    public String getSoyad() {
        return soyad;
    }

    public void setSoyad(String soyad) {
        this.soyad = soyad;
    }

    public String getBolum() {
        return bolum;
    }

    public void setBolum(String bolum) {
        this.bolum = bolum;
    }

    public String getUniversite() {
        return universite;
    }

    public void setUniversite(String universite) {
        this.universite = universite;
    }

    public String getResimUrl() {
        return resimUrl;
    }

    public void setResimUrl(String resimUrl) {
        this.resimUrl = resimUrl;
    }
}
Adapter > ListeAdapter.java
package com.serifgungor.ogrencilerjson.Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
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.StringRequest;
import com.android.volley.toolbox.Volley;
import com.serifgungor.ogrencilerjson.Adapter.ListeAdapter;
import com.serifgungor.ogrencilerjson.Model.Ogrenci;
import com.serifgungor.ogrencilerjson.R;

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

import java.util.ArrayList;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    ListView listView;
    ArrayList ogrenciler = new ArrayList<>();
    ListeAdapter listeAdapter;
    Button btn;

    RequestQueue istekKuyrugu;

    public void ogrencileriCek(String url){

        StringRequest istek = new StringRequest(

                Request.Method.POST,
                url,
                new Response.Listener() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("LOG",response);

                        try {
                            JSONObject object = new JSONObject(response);
                            JSONArray array = object.optJSONArray("ogrenciler");

                            for(int i=0; i < array.length(); i++){
                                JSONObject item = array.optJSONObject(i);
                                int ogrenciNo = item.getInt("ogrenciNo");
                                String ad = item.getString("ad");
                                String soyad = item.getString("soyad");
                                String bolum = item.getString("bolum");
                                String universite = item.getString("universite");
                                String resimUrl = item.getString("resimUrl");
                                Log.d("RESP",""+soyad);

                                ogrenciler.add(
                                        new Ogrenci(
                                                ogrenciNo,
                                                ad,
                                                soyad,
                                                bolum,
                                                universite,
                                                resimUrl)
                                );


                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                },

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

                    }
                }

        ){
            @Override
            protected Map< String, String > getParams() throws AuthFailureError {
                return super.getParams();
            }
        };

        istekKuyrugu.add(istek);
    }


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


        istekKuyrugu = Volley.newRequestQueue(getApplicationContext());

        btn = findViewById(R.id.button2);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listeAdapter = new ListeAdapter(MainActivity.this,ogrenciler);
                listView.setAdapter(listeAdapter);
            }
        });

        ogrencileriCek("http://serifgungor.com/Ogrenciler.json");


    }
}
Layout > custom_row.xml
< ?xml version="1.0" encoding="utf-8"?>
< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    < ImageView
        android:id="@+id/ivResim"
        android:layout_width="130dp"
        android:layout_height="150dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        app:srcCompat="@mipmap/ic_launcher" />

    < TextView
        android:id="@+id/tvAd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:layout_toEndOf="@+id/ivResim"
        android:text="TextView" />

    < TextView
        android:id="@+id/tvSoyad"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvAd"
        android:layout_margin="10dp"
        android:layout_toEndOf="@+id/ivResim"
        android:text="TextView" />

    < TextView
        android:id="@+id/tvBolum"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvSoyad"
        android:layout_margin="10dp"
        android:layout_toEndOf="@+id/ivResim"
        android:text="TextView" />

    < TextView
        android:id="@+id/tvUniversite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvBolum"
        android:layout_margin="10dp"
        android:layout_toEndOf="@+id/ivResim"
        android:text="TextView" />
< /RelativeLayout>

Layout > activity_main.xml

< ?xml version="1.0" encoding="utf-8"?>
< RelativeLayout 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=".Activity.MainActivity">

    < ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="445dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    < Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:text="Doldur" />
< /RelativeLayout>
Bu makaleyi paylaşın:
İstanbul'da yaşıyorum.Web üzerinde Frontend/Backend Geliştirmenin yanı sıra, Android Geliştirme, PHP/ASP.NET, C# Yazılım Uzmanlığı ve Java konuları işliyorum. Kod Şiirdir, onları seviyorum.

Bir yorum bırakın