Covid 19 Get All Country Data using Volley library

 

Covid 19 Get All Country Data using Volley library


Output Screen






add dependency in gradle
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'

MainActivity.java
package com.example.livejsonparsing;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
RecyclerView recyclerView;
CustomCaseAdapter customCaseAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progress);
recyclerView = findViewById(R.id.myrecyclerview);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
customCaseAdapter = new CustomCaseAdapter(caseList);

customCaseAdapter.setCaseListener(new CustomCaseAdapter.CaseListener() {
@Override
public void caseItem(View view, int position) {
Case ca = caseList.get(position);
AlertDialog.Builder a = new AlertDialog.Builder(MainActivity.this);
a.setTitle(ca.getCountry());
a.setMessage("Total Death " + ca.getDeaths());
a.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
a.show();
}
});

recyclerView.setAdapter(customCaseAdapter);
}

private List<Case> caseList = new ArrayList<>();

public void getData(View view) {
progressBar.setVisibility(View.VISIBLE);
StringRequest sr = new StringRequest(Request.Method.GET,
"https://coronavirus-19-api.herokuapp.com/countries",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressBar.setVisibility(View.GONE);

try {
JSONArray jsonArray = new JSONArray(response);
Gson gson = new Gson();
for (int i = 0; i < jsonArray.length(); i++) {
Case cs = gson.fromJson(jsonArray.
getJSONObject(i).
toString(), Case.class);
caseList.add(cs);
}
} catch (Exception e) {
Log.e("error", e.toString());
}
customCaseAdapter.notifyDataSetChanged();

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressBar.setVisibility(View.GONE);
Log.e("error", error.toString());
Toast.makeText(MainActivity.this, error.toString(),
Toast.LENGTH_SHORT).show();
}
});
Volley.newRequestQueue(MainActivity.this).add(sr);
}


}

Case.java
package com.example.livejsonparsing;

public class Case {
private String country;
private int cases;
private int todayCases;
private int deaths;
private int todayDeaths;
private int recovered;
private int active;
private int critical;

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public int getCases() {
return cases;
}

public void setCases(int cases) {
this.cases = cases;
}

public int getTodayCases() {
return todayCases;
}

public void setTodayCases(int todayCases) {
this.todayCases = todayCases;
}

public int getDeaths() {
return deaths;
}

public void setDeaths(int deaths) {
this.deaths = deaths;
}

public int getTodayDeaths() {
return todayDeaths;
}

public void setTodayDeaths(int todayDeaths) {
this.todayDeaths = todayDeaths;
}

public int getRecovered() {
return recovered;
}

public void setRecovered(int recovered) {
this.recovered = recovered;
}

public int getActive() {
return active;
}

public void setActive(int active) {
this.active = active;
}

public int getCritical() {
return critical;
}

public void setCritical(int critical) {
this.critical = critical;
}
}

CustomCaseAdapter.java
package com.example.livejsonparsing;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class CustomCaseAdapter extends RecyclerView.Adapter
<CustomCaseAdapter.MyViewHolder> {
private List<Case> caseList;
private CaseListener caseListener;

public CustomCaseAdapter(List<Case> caseList) {
this.caseList = caseList;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).
inflate(R.layout.case_item, parent, false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Case ca = caseList.get(position);
holder.tcountry.setText(ca.getCountry());
holder.tcase.setText(String.valueOf(ca.getCases()));
Log.e("error", caseList.size() + "");
}

@Override
public int getItemCount() {
return caseList.size();
}

class MyViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
TextView tcountry, tcase;

public MyViewHolder(@NonNull View itemView) {
super(itemView);
tcountry = itemView.findViewById(R.id.countryname);
tcase = itemView.findViewById(R.id.countrycase);
itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
caseListener.caseItem(view, getAdapterPosition());
}
}

public void setCaseListener(CaseListener caseListener) {
this.caseListener = caseListener;
}

public interface CaseListener {
void caseItem(View view, int position);
}

}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.livejsonparsing">

<uses-permission-sdk-23 android:name="android.permission.INTERNET" />
<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>

6 comments:

MH Zibon said...

Very good written article. It will be supportive to anyone who utilizes it, including me. Keep doing what you are doing – can't wait to read more posts.the complete guide

Anonymous said...

I am so much excited after reading your blog. Your blog is very much innovative and much helpful for any industry as well as in person.

flutter programmierer

Buy Online said...


Being cute is a way of life for this lively charmer. The Shih Tzu is known to be especially affectionate with children. As a small dog bred to spend most of their day inside royal palaces, they make a great pet if you live in an apartment or lack a big backyard.
where to buy Shih Tzu puppies

Buy Online said...


We offer great deals and expert service.Our specialty are on gun brands like the COLT,which include,the colt rifles,revolvers,colt pistols,Taurus Handguns and Panzer arms.We have the latest collection of the AR-12 Shotguns,panzer bp-12,panzer arms ar12 pro. USA GUN FACTORY is a federally licensed firearms dealer, Visit now

Taurus Tracker

Anonymous said...

I seriously love your blog.. Pleasant colors & theme.
Did you build this amazing site yourself? Please reply back as I’m trying to create my own personal blog and would like to
learn where you got this from or what the theme is named. Many thanks!
order muha meds carts online

Anonymous said...

I quite like reading through an article that can make men and women think.
Also, many thanks for allowing for me to comment!
Buy Vyvanse Online cheap and affordable with or without prescription