Json data array request keeps skipping onResponse

2019-08-12 17:59发布

I keep trying to look up and find a solution for my problem.

I am populating a Spinner with json data from a URL, the Json data is nameless and I'm trying to get the data into an ArrayList so I can populate the Spinner.

Whenever I debug I can see that it skips my onResponse() method after making either StringRequest or JSONArrayRequest, Please look at my code and see what am I doing wrong

yearsp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if(ySelect)
            {
                selectedYear = yearsp.getSelectedItem().toString();

                URL1 = URL + "/" + selectedYear;
                loadSpinnerDataMake();

                adapterMake.clear();
                adapterMake.addAll(makesName);

            }
            else
                ySelect = true;

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });


String URL1 = http://carmakemodeldb.com/vehicle/makes/2011 //this is here for you to see the link
public void loadSpinnerDataMake()
{
    makesName = new ArrayList<String>();

    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(URL1, new Response.Listener<JSONArray>() {
    @Override
    public void onResponse(JSONArray response) {
        try {

            for (int x = 0; x < response.length(); x++) {
                JSONObject obMake = response.getJSONObject(x);
                String nMake = obMake.getString("make");
                makesName.add(nMake);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();

        }
    });


    requestQueue.add(jsonArrayRequest);

}

here is my json data from URL1

[{"make":"Acura"},{"make":"Aston Martin"},{"make":"Audi"},{"make":"Bentley"},{"make":"BMW"},{"make":"Bugatti"},{"make":"Buick"},{"make":"Cadillac"},{"make":"Chevrolet"},{"make":"Chrysler"}]

3条回答
欢心
2楼-- · 2019-08-12 18:19

I fixed my issue thank you everyone who has helped but i figured it out by removing where im adding the arraylist to the adapter and placing it in the OnResponse

public void loadSpinnerDataMake()
{

    makesName = new ArrayList<String>();

    JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(URL1, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            try {
                JSONArray jsonArray = response;
                for (int x = 0; x < jsonArray.length(); x++) {
                    JSONObject obMake = jsonArray.getJSONObject(x);
                    String nMake = obMake.getString("make");
                    makesName.add(nMake);

                    adapterMake.clear();
                    adapterMake.addAll(makesName);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();

        }
    });

    RequestQueue requestQueue = Volley.newRequestQueue(getContext());

    requestQueue.add(jsonObjectRequest);

}
查看更多
我命由我不由天
3楼-- · 2019-08-12 18:35

Try below function and let me know.

private void parseJSONDataMAke() {
        try {
            JSONArray dataMakeArray = new JSONArray("[{\"make\":\"Acura\"},{\"make\":\"Aston Martin\"},{\"make\":\"Audi\"},{\"make\":\"Bentley\"},{\"make\":\"BMW\"},{\"make\":\"Bugatti\"},{\"make\":\"Buick\"},{\"make\":\"Cadillac\"},{\"make\":\"Chevrolet\"},{\"make\":\"Chrysler\"}]");

            ArrayList<String> stringArrayList = new ArrayList<>();

            for (int i = 0; i < dataMakeArray.length(); i++) {
                JSONObject jsonObject = dataMakeArray.getJSONObject(i);
                stringArrayList.add(jsonObject.getString("make"));
            }

            //Please set "stringArrayList" array into spinner

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
查看更多
贪生不怕死
4楼-- · 2019-08-12 18:40

Your request is async hence you need to notify your spinner adapter when you get response like this.

String URL1 = http://carmakemodeldb.com/vehicle/makes/2011 //this is here for you to see the link
public void loadSpinnerDataMake()
{
    makesName = new ArrayList<String>();

    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(URL1, new Response.Listener<JSONArray>() {
    @Override
    public void onResponse(JSONArray response) {
        try {
            makesName.clear(); //To avoid duplication of data
            for (int x = 0; x < response.length(); x++) {
                JSONObject obMake = response.getJSONObject(x);
                String nMake = obMake.getString("make");
                makesName.add(nMake);
            }

            adapterMake.notifyDataSetChanged();

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();

        }
    });


    requestQueue.add(jsonArrayRequest);

}

You are not notifying adapter that's why you didn't get your result.

查看更多
登录 后发表回答