Search code examples
androidtoast

Why Android Toast is not showing?


I've a problem on gettingToast

public class LiveMatch extends Fragment {

    private List<Items_list> matches;

    private static final String URL="https://www.cricbuzz.com/match-api/livematches.json";

    private String recent_match;


    View v;

    public LiveMatch() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.live, container, false);

        return v;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        matches = new ArrayList<>();
        final StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray live_match = new JSONArray(response);
                    Toast.makeText(v.getContext(),live_match.length(),Toast.LENGTH_LONG).show();
                    for (int i = 0; i < live_match.length(); i++) {
                        JSONObject object = live_match.getJSONObject(i);

                        recent_match = object.getString("mathes");

                        RecyclerView recyclerView = v.findViewById(R.id.recycl);
                        RecylerAddapter recylerAddapter = new RecylerAddapter(getContext(), matches);
                        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                        recyclerView.setAdapter(recylerAddapter);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(v.getContext(), error.toString(), Toast.LENGTH_SHORT).show();
                    }
                });

        Volley.newRequestQueue(Objects.requireNonNull(getContext())).add(stringRequest);
}
}

Solution

  • The problem is in Parsing JSON response.

    Web service(API) response comes in JSONObject and you are handling it in JSONArray.
    See the response of Livestream URL

    enter image description here

    Correct your code of handling response and it will work.

    Take note that you have to get values by iterating the loop using Iterator because matches object contains JSONObjects.

    Update your onResponse method as below:

    @Override
    public void onResponse(String response) {
        try {
            JSONObject jsonObject = new JSONObject(response);
            Log.e("Response: ", jsonObject.toString());
            JSONObject objMatch = jsonObject.optJSONObject("matches");
            Iterator<String> iterator = objMatch.keys();
            int counter = 0;
    
            while (iterator.hasNext()) {
                String key = iterator.next();
                try {
                    JSONObject value = objMatch.getJSONObject(key);
                    Log.e("Value: ", value.toString());
                    // Here you have to add values in Items_list  and then after add it in matches list
                    counter++;
                } catch (JSONException e) {
                    // Something went wrong!
                }
            }
            Toast.makeText(getActivity(), "Total Records: " + counter, Toast.LENGTH_LONG).show();
            // After competition of iterator loop, you have to set that List in RecyclerView Adapter here
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }