2

I am trying to get values from a json data with AsyncTask. I am getting only the last value and I don't understand why...

I tryed to parse with for each, while but I am doing something wrong :

Here is my code :

private class DecodeData extends AsyncTask<String, Void, String> {

    protected       ArrayList<HashMap<String, String>>    decodedArray;
    protected       HashMap<String, String> decodedMap;
    protected       Iterator<String>        it;
    protected       JSONArray               m_Array;
    protected       JSONObject              object;
    protected       String response;
    protected       String keys;
    protected       String value;

    @SuppressWarnings("unchecked")
    @Override
    protected String doInBackground(String... params) {
        response = params[0];
        keys = "";
        value = "";
        object = null;
        decodedArray = new ArrayList<HashMap<String, String>>();
        try {
            JSONArray arrayResp = new JSONArray(response);
            for (int i = 0; i < arrayResp.length(); i++) {
                decodedMap = new HashMap<String, String>();
                it = arrayResp.getJSONObject(i).keys();
                while (it.hasNext()) {
                    keys = (String)it.next();
                    value = Base64.DecodeStrToStr((String)arrayResp.getJSONObject(i).get(keys));
                    decodedMap.put("\""+keys+"\"", "\""+value+"\"");

                    object = new JSONObject(decodedMap.toString());
                    Log.i("DECODED MAP : ", object.toString());
                    m_Array = new JSONArray();
                    m_Array.put(object);
                    Log.i("M_ARRAY", ""+m_Array);
                }
                //                  decodedArray.add(decodedMap);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //          array = new JSONArray(decodedArray);
        return m_Array.toString();
    }

I am using Volley to get response. After that, I create a JSONArray with this response and I get all keys/values from it. I put all of them in my Hashmap. But when i'm putting keys/values here : m_Array.put(object), it puts only the last value of my json data. Anybody has an idea of what I'm making wrong ?

3
  • 2
    Could it be because you are creating the map in the for loop, thus creating a new map on each iteration - this is how the old entries get lost. Also another thing to consider: putting a key-value-pair in a map with the same key twice will 'overwrite' the previous pair Commented May 14, 2014 at 15:41
  • @Anton It changes nothing.. I tryed what you said Commented May 14, 2014 at 15:47
  • have you tried creating m_Array outside of the loop, too? Right now it is created again on every iteration Commented May 14, 2014 at 15:49

1 Answer 1

6

Please create JSONArray before starting for loop..

m_Array = new JSONArray();
JSONArray arrayResp = new JSONArray(response);
for (int i = 0; i < arrayResp.length(); i++) { ....
Sign up to request clarification or add additional context in comments.

2 Comments

It's funny how you (@Fcps) said you tried Anton's solution and it didn't work, and yet this is exactly what he said to do...
@Takendarkk He said I created my map in the for loop, it was the JSONArray so it isn't the same to me. Sorry for my no understanding.

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.