Covid 19 Track By Country Name
add dependency in gradle:
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.google.code.gson:gson:2.8.5'
MainActivity.java
package com.example.livejsonparsing;
import androidx.appcompat.app.AppCompatActivity;
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;
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
TextView tdata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progress);
tdata = findViewById(R.id.mydata);
}
public void getData(View view) {
String country="india";
progressBar.setVisibility(View.VISIBLE);
StringRequest sr = new StringRequest(Request.Method.GET,
"https://coronavirus-19-api.herokuapp.com/countries/"+country,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressBar.setVisibility(View.GONE);
//Toast.makeText(MainActivity.this,response, Toast.LENGTH_SHORT).show();
Gson gson = new Gson();
Case cc = gson.fromJson(response, Case.class);
if (cc != null) {
tdata.setText("Active Case : " + cc.getActive() + "\n" +
"Country : " + cc.getCountry() + "\n" + " Cases : "+ cc.getCases() +
"\n" + "Death Case : " + cc.getDeaths() + "\n");
}
}
}, 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;
}
}
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>
No comments:
Post a Comment