Json Persing using Volley
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"
android:id="@+id/activity_main"
tools:context="com.example.abhinandan.demojsonparsing.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv">
</ListView>
</RelativeLayout>
row.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"><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvcountryname"
android:textSize="25dp"/><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvcapital"
android:textSize="25dp"/></LinearLayout>
DemoSetGet.java
package com.example.abhinandan.demojsonparsing;/** * Created by Abhinandan on 12/1/2017. */public class DemoSetGet {String countryname,capital,currencyname;public String getCountryname() {return countryname;}public void setCountryname(String countryname) {this.countryname = countryname;}public String getCapital() {return capital;}public void setCapital(String capital) {this.capital = capital;}public String getCurrencyname() {return currencyname;}public void setCurrencyname(String currencyname) {this.currencyname = currencyname;}}MainActivity.java
package com.example.abhinandan.demojsonparsing;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;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 org.json.JSONArray;import org.json.JSONObject;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {String urlJsonArray="http://www.wptrafficanalyzer.in/p/demo1/first.php/countries/";ArrayList<DemoSetGet> arrayList=new ArrayList<>();@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);sendRequest();}public class MyAdapter 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 inflater = getLayoutInflater();convertView = inflater.inflate(R.layout.row, parent, false);TextView tvcountryname = (TextView) convertView.findViewById
(R.id.tvcountryname);tvcountryname.setText(arrayList.get(position).getCountryname());TextView tvcapital = (TextView) convertView.findViewById(R.id.tvcapital);tvcapital.setText(arrayList.get(position).getCapital());return convertView;}private void sendRequest()}{StringRequest stringRequest=new StringRequest(urlJsonArray,
new Response.Listener<String>() {@Override public void onResponse(String response) {showJson(response);}},new Response.ErrorListener(){@Override public void onErrorResponse(VolleyError error)
{Toast.makeText(MainActivity.this,error.getMessage(),
Toast.LENGTH_LONG).show();}});RequestQueue requestQueue = Volley.newRequestQueue(this);requestQueue.add(stringRequest);}public void showJson(String json){try {JSONObject job=new JSONObject(json);JSONArray jarr=job.getJSONArray("countries");for(int i=0;i<jarr.length();i++){JSONObject j=jarr.getJSONObject(i);DemoSetGet s=new DemoSetGet();s.setCountryname(j.getString("countryname"));s.setCapital(j.getString("capital"));arrayList.add(s);}}catch (Exception e){e.printStackTrace();}ListView lv=(ListView)findViewById(R.id.lv);lv.setAdapter(new MyAdapter());}We have to give permissions for Internet access in Manifest file-}AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abhinandan.demojsonparsing"><uses-permission android:name="android.permission.INTERNET"></uses-permission><application android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>After that go to Gradle Scripts and then open build.gradle(Module:app)-
Paste- "compile 'com.mcxiaoke.volley:library-aar:1.0.0'"
Comments
Post a Comment