-1

I understand that what I'm fetching is already an array but still I'm not sure what to change here.

I have this which is returning data fine but I'm not sure what to do with the array to get the values into the map.

protected Void doInBackground(Void... params) {
        // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONfunctions
                .getJSONfromURL("http://www.mywebsite.club/api/coffees");

        try {
            // Locate the array name in JSON
            jsonarray = jsonobject.getJSONArray("coffees");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                // Retrive JSON Objects
                map.put("title", jsonobject.getString("title"));
                map.put("brand", jsonobject.getString("brand"));
                map.put("price", jsonobject.getInt("price"));
                map.put("brandlogo", jsonobject.getString("brandlogo"));
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
8
  • Paste the server response you want to parse Commented Feb 25, 2016 at 15:20
  • You can just append json array in response from the url. "{coffees:" + url_reponse + "}" after that u can go ahead and get jsonarray as coffees Commented Feb 25, 2016 at 15:30
  • From your response, I didn't find coffees attribute as array name, in your json response you have only array without attribute name. So you don't need to get json object from response, whatever your response string just get json array from that like, JSONArray jsonarray = new JSONArray(jsonobject.toString()); Commented Feb 25, 2016 at 15:30
  • user370305 please post it as an aswer because it's right :) and comments are not for answering Commented Feb 25, 2016 at 15:30
  • @bartol - I knew, let just OP give chance to try this, if he will get success will post as answer. Thanks :) Commented Feb 25, 2016 at 15:32

1 Answer 1

1

From your response, I didn't find coffees attribute as array name, in your json response you have only array without attribute name. So you don't need to get json object from response, whatever your response string just get json array from that like,

protected Void doInBackground(Void... params) {
    // Create an array
    arraylist = new ArrayList<HashMap<String, String>>();
    // Retrieve JSON Array from the given URL address
    jsonarray = new JSONArray(JSONfunctions.getJSONfromURL("http://www.mywebsite.club/api/coffees"));

    try {
         if(jsonarray != null)
         {

          for (int i = 0; i < jsonarray.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            jsonobject = jsonarray.getJSONObject(i);
            // Retrive JSON Objects
            map.put("title", jsonobject.getString("title"));
            map.put("brand", jsonobject.getString("brand"));
            map.put("price", jsonobject.getInt("price"));
            map.put("brandlogo", jsonobject.getString("brandlogo"));
            // Set the JSON Objects into the array
            arraylist.add(map);
          }
       }
    } catch (JSONException e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    return null;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.