The first map gives the size 3, everything is ok. But the one that is filled from the database always has a size of 0, there are no duplicate keys and values. The data from the database itself is normal.

hashmap.put(1,"1"); hashmap.put(2,"2"); hashmap.put(3,"3"); hashmap.size(); requestQueue= Volley.newRequestQueue(this); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try{ JSONArray jsonArray = response.getJSONArray("JUDGES"); for (int i=0;i>jsonArray.length();i++){ JSONObject judges = jsonArray.getJSONObject(i); String name = judges.getString("J_NAME"); int jid = judges.getInt("ID"); hmjudges.put(jid,name); hmjudges.size(); } }catch (JSONException e){e.printStackTrace();} } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); requestQueue.add(jsonObjectRequest); 

UPD With ArrayList everything works.

 hashmap.put(1,"1"); hashmap.put(2,"2"); hashmap.put(3,"3"); hashmap.size(); final List<String> names = new ArrayList<String>(); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, "http://full-version.ru/select_judge/judges.php", new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try{ JSONArray jsonArray = response.getJSONArray("JUDGES"); for (int i=0;i<jsonArray.length();i++){ JSONObject judges = jsonArray.getJSONObject(i); String name = judges.getString("J_NAME"); int jid = judges.getInt("ID"); names.add(name); hmjudges.put(jid,name); hmjudges.size(); } }catch (JSONException e){e.printStackTrace();} } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); requestQueue= Volley.newRequestQueue(this); requestQueue.add(jsonObjectRequest); 

    1 answer 1

    The author was sealed in a cycle:

     for (int i=0;i>jsonArray.length();i++){ 

    It will be right:

     for (int i = 0; i < jsonArray.length(); i++) { 
    • I also thought that I was mistaken, but I didn’t change places - nothing changed) - Andrew Dmitriev
    • @AndrewDmitriev, it means that jsonArray is empty or onResponse was not called at all. Add logging to the code and see where the code was executed - gil9red
    • This is the essence that the String name and int jid are filled! I added logs and output everything, but for some reason I don’t add them to the map. In the arraylist, everything is fine added, but added a hashmap - everything stopped working) - Andrew Dmitriev
    • Then, or you have somewhere in the code, it is cleared, or reinitialized, and you, judging by the code, you add to hmjudges , and not a hashmap , can therefore hashmap and be empty? :) - gil9red
    • hmjudges is a hashmap that receives data from the database, and that hashmap that is above, just for example, that everything works. - Andrew Dmitriev