-4

I have a JSON file with just one Array as in this example (as you see this array doesn't have a name):

["Cream","Cheese","Milk","Powder Milk","Blue Cheese","Gouda Cheese"]

How can I just get this array into an array or an arraylist in my Android Studio?

Thank you so much in advance!

Here is a function that I am creating to get this array yet I can't complete it I am still a noob with handling json files.

    public void getProducts() {


    JsonArrayRequest myresponseArray = new JsonArrayRequest(url,

            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    try {

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

                            JSONArray responseArray = response.getJSONArray(i);
                        }

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

                }
            });


}

Now I want to add this "responseArray" into my products array list that I created on the MainActivity

ArrayList<String> products = new ArrayList<String>();
Mazzin
  • 21
  • 2

1 Answers1

0

Try with geString(int index)

for (int i = 0; i < responeArray.length(); i++) {
   products.add( responseArray.getString(i) );
}
Adolfo Esparza
  • 133
  • 1
  • 8
  • You are the boss! I just took the response directly with getString as the response was already an array as in `products.add( response.getString(i) );` Thank you so much. – Mazzin Jul 04 '18 at 15:43