سؤال

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 ?

هل كانت مفيدة؟

المحلول

Please create JSONArray before starting for loop..

m_Array = new JSONArray();
JSONArray arrayResp = new JSONArray(response);
for (int i = 0; i < arrayResp.length(); i++) { ....
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top