0

I wrote this code:

private ArrayList<HashMap<String, HashMap<Integer, String>>> listContent2 = new ArrayList<HashMap<String, HashMap<Integer, String>>>();

public ArrayList<HashMap<String, HashMap<Integer, String>>> content() {
    JSONObject json = JSONfunctions.getJSONfromURL("http://...");
    try {
        JSONArray hotspots = json.getJSONArray("hotspots");
        HashMap<String, HashMap<Integer, String>> mapContentHotspot = new HashMap<String, HashMap<Integer, String>>();
        for (int i = 0; i < hotspots.length(); i++) {
            JSONObject e = hotspots.getJSONObject(i);
            JSONArray actions = new JSONArray(e.getString("actions"));

            for (int j = 0; j < actions.length(); j++) {
                JSONObject e2 = actions.getJSONObject(j);
                HashMap<Integer, String> mapContent = new HashMap<Integer, String>();

                switch (e2.getInt("activityType")) {
                    case 27:
                        mapContent.put(e2.getInt("activityType"), e2.getString("uri"));
                    case 2:
                        mapContent.put(e2.getInt("activityType"), e2.getString("uri"));
                    case 1:
                        mapContent.put(e2.getInt("activityType"), e2.getString("uri"));
                    //default:
                        //break;
                }

                mapContentHotspot.put(e.getString("id"), mapContent);
            }

            listContent2.add(mapContentHotspot);
        }
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
    return listContent2;
}

to take the content of this json array: http://pastebin.com/bXfwcQ2U The problem is in the section of "actions". I try to take the 3 "uri" but i take only the last one (with the "activityType": "1"). Where is my wrong in my java code? thanks!

0

Maybe mapContent creation should be moved outside of the innermost loop.

| improve this answer | |
0

You have to add break !!! in your code

case 27:
                    mapContent.put(e2.getInt("activityType"), e2.getString("uri"));
                    break;
                case 2:
                    mapContent.put(e2.getInt("activityType"), e2.getString("uri")); 
                    break;
                case 1:
                    mapContent.put(e2.getInt("activityType"), e2.getString("uri"));
                    break;
| improve this answer | |
  • Usually, yes. But in this case it won't make a difference, because all the cases are the same, and idempotent. – Thilo Aug 17 '11 at 13:12
  • exactly what it says Thilo!! with the "break" it doesn't work! – John Aug 17 '11 at 13:22
  • @John: You should still have the break. Or even better combine the cases into one (to remove code duplication): case 27: case 2: case1: mapContent.put ... break; – Thilo Aug 17 '11 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.