Search code examples
javaandroidjsonapiandroid-volley

How can I Convert JSON to ARRAY Response GET from api Request In Android Java


I am tring to fetch data and but its show nothing. even when I am trying to print on console same things happend

 JsonObjectRequest jsonRequest = new JsonObjectRequest
            (Request.Method.GET, onewayUrl, null, response -> {


                try {
                    JSONArray jsonArray = new JSONArray(response);

                    for (int i = 0; i < jsonArray.length(); i++) {

                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String Career = jsonObject.getString("career");

                        textView = findViewById(R.id.text);
                        textView.setText(Career);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }, error -> {
                error.printStackTrace();
            });

    Volley.newRequestQueue(AfterSearch.this).add(jsonRequest);
    jsonRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f));

I got Some error looks like.

W/System.err:     at org.json.JSON.typeMismatch(JSON.java:112)
W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:169)
W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:182)
W/System.err:     at 
com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:98)  

Solution

  •  String url = "https://api.flyfarint.com/v.1.0.0/AirSearch/oneway.php?tripType=oneway&journeyfrom=DAC&journeyto=DXB&departuredate=2022-10-30&adult=1&child=0&infant=0";
        private ProgressDialog pDialog;
       pDialog = new ProgressDialog(this);
            // Showing progress dialog before making http request
            pDialog.setMessage("Loading...");
            pDialog.show();
            RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
    
            JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, response -> {
                hidePDialog();
                for (int i = 0; i < response.length(); i++) {
    
                    try {
                        JSONObject responseObj = response.getJSONObject(0); //showing here only one using recyclerview you can show all.USE "i" for all records with model.
                        String careerName = responseObj.getString("careerName");
                        TextView textView=findViewById(R.id.text);
                        textView.setText(careerName);
    
                    } catch (JSONException e) {
                        e.printStackTrace();
                        hidePDialog();
                    }
                }
            }, error -> hidePDialog());
            jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f));
            queue.add(jsonArrayRequest);
        }
        @Override
        public void onDestroy() {
            super.onDestroy();
            hidePDialog();
        }
    
        private void hidePDialog() {
            if (pDialog != null) {
                pDialog.dismiss();
                pDialog = null;
            }
        }