1

I am working on an android project that will get a Json array from a server and then I want to iterate through the array and extract data from each Json object in the array. When I run this code the textviews do not change, but if I remove the for loop and everything in it, and then uncomment the two lines below the for loop, things seems to work. How can you iterate through the Json objects in the array so that the info will be updated on the screen and change every second for each object in the array?

Thank you in advance!

    name = (TextView)findViewById(R.id.nameTxt);
    email = (TextView)findViewById(R.id.emailTxt);
    serverButton = (Button)findViewById(R.id.getInfoBtn);


    serverButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(server_url,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            try {

                                for (int i = 0; i < response.length(); i++) {
                                    JSONObject nameJson = response.getJSONObject(i);
                                    name.setText(nameJson.getString("FirstName") + " " + nameJson.getString("LastName"));
                                    email.setText(nameJson.getString("Email"));
                                    Thread.sleep(1000);
                                }

                                //JSONObject nameJson = response.getJSONObject(1);
                               // name.setText(nameJson.getString("FirstName"));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    name.setText("Something is wrong");
                }
            });

            MySingleton.getInstance(MainActivity.this).addToRequestQueue(jsonArrayRequest);
topgun741
  • 135
  • 1
  • 11
  • My guess would be the `Thread.sleep` is causing some problems here. Have you tried the for loop without the `Thread.sleep` just to see if anything is changing? – akousmata Dec 20 '16 at 20:32
  • You should be able to take the general approach in this answer and apply it to your situation. Looks like the OP in this question had problems with using `Thread.sleep` as I suspected. http://stackoverflow.com/a/6242292/567547 – akousmata Dec 20 '16 at 20:35
  • I have tried removing the thread.sleep and it make no difference. So I assume it is not causing the problem. – topgun741 Dec 20 '16 at 20:41
  • Is there anything in the logs? E.g a caught exception? – Isaac Dec 20 '16 at 21:18
  • Thank you Isaac Payne! I found out that there was a small bug in my server side code after checking the logs. – topgun741 Dec 20 '16 at 21:51

0 Answers0