Android Studio Java JSON fetching data problem

PLEASE HELP, WHAT IS WRONG WITH MY CODE, IT DISPLAYS NOTHING BUT ZEROS AND NULL

I already search through tutorials, i even copied the whole code but i cant get the data from this API, my textviews on int are returning zeros and my textviews on string are returning null after a press the update button.

here is my api link = api link

here is the result

package com.example.firstapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

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


public class MainActivity extends AppCompatActivity {

private TextView confirmed, recovered, deaths, country, date;
private RequestQueue mQueue;
private Button update;
private int c,r,d;
private String co,da;

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

    confirmed = findViewById(R.id.confirmed);
    recovered = findViewById(R.id.recovered);
    deaths = findViewById(R.id.deaths);
    country = findViewById(R.id.country);
    date = findViewById(R.id.date);
    update = findViewById(R.id.update);

    mQueue = Volley.newRequestQueue(this);


    update.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            jsonParse();
            confirmed.setText(String.valueOf(c));
            recovered.setText(String.valueOf(r));
            deaths.setText(String.valueOf(d));
            country.setText(co);
            date.setText(da);
        }
    });

}

private void jsonParse(){

    String url = "https://covid-api.mmediagroup.fr/v1/cases?country=Philippines";
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {

                        JSONArray jsonArray = response.getJSONArray("All");
                        for (int i = 0; i < jsonArray.length(); i++) {

                            JSONObject cases = jsonArray.getJSONObject(i);
                            c = cases.getInt("confirmed");
                            r = cases.getInt("recovered");
                            d = cases.getInt("deaths");
                            co = cases.getString("country");
                            da = cases.getString("updated");

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(),
                    error.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    });
    mQueue.add(request);
}
}

I don't know what is the problem here, can someone help

1 answer

  • answered 2021-05-14 19:21 sentrax

    I already fixed it thank you so much.

    my solution is I removed JsonArray.

    package com.example.firstapp;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    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.JsonObjectRequest;
    import com.android.volley.toolbox.Volley;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    
    public class MainActivity extends AppCompatActivity {
    
    private TextView confirmed, recovered, deaths, country, date;
    private RequestQueue mQueue;
    private Button update;
    private int c,r,d;
    private String co,da;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        confirmed = findViewById(R.id.confirmed);
        recovered = findViewById(R.id.recovered);
        deaths = findViewById(R.id.deaths);
        country = findViewById(R.id.country);
        date = findViewById(R.id.date);
        update = findViewById(R.id.update);
    
        mQueue = Volley.newRequestQueue(this);
    
    
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jsonParse();
    
            }
        });
    
    }
    
    private void jsonParse(){
    
        String url = "https://covid-api.mmediagroup.fr/v1/cases?country=Philippines";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, 
    new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try{
    
                            //JSONArray jsonArray = response.getJSONArray();
                            for (int i = 0; i < response.length(); i++) {
    
                                JSONObject cases = response.getJSONObject("All");
                                c = cases.getInt("confirmed");
                                r = cases.getInt("recovered");
                                d = cases.getInt("deaths");
                                co = cases.getString("country");
                                da = cases.getString("updated");
                                confirmed.setText(String.valueOf(c));
                                recovered.setText(String.valueOf(r));
                                deaths.setText(String.valueOf(d));
                                country.setText(co);
                                date.setText(da);
    
    
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(),
                        error.getMessage(),
                        Toast.LENGTH_LONG).show();
            }
        });
        mQueue.add(request);
    }
    }
    

    I replaced JSONObject cases = jsonArray.getJSONObject(i); with JSONObject cases = response.getJSONObject("All"); and removed JSONArray jsonArray = response.getJSONArray(); because it is not necessary.