Create objects after external server response via API

0

It is the following, I created an API in php that generates a Json. LINK is this for anyone who wants to view.

I was able to access the link and retrieve the information, as I used a Toast to show them. But for some reason, which I do not know, I can not use this information to create objects.

Below is the function I'm using inside MainActivity:

listaHome = new ArrayList<>();

    RequestQueue request = Volley.newRequestQueue(getApplicationContext());

    JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, "http://qsmidia.com.br/androidTeste/", null,new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            try {
                JSONArray postagens = response.getJSONArray("anotacao");
                for (int i = 0; i < postagens.length(); i++){
                    JSONObject post = postagens.getJSONObject(i);
                    // aqui ele deveria adicionar a lista
                    listaHome.add(new Anotacoes(post.getString("id_anotacao"), post.getString("titulo"), post.getString("texto")));
                    // aqui ele mostra o título no toast (2 registros)
                    Toast.makeText(getApplicationContext(), post.getString("titulo"), Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.i("Erro: ", e.getMessage());
                Toast.makeText(getApplicationContext(), "Erro exception > " + e.toString(), Toast.LENGTH_LONG).show();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), "Erro de resposta > " + error.getMessage(), Toast.LENGTH_LONG).show();
        }
      }
    );
    request.add(objectRequest);

This listaHome is declared as private List<Anotacoes> listaHome; within the same function class.

Before you ask me I inserted it into the manifest:

<uses-permission android:name="android.permission.INTERNET" />

And in the dependencies:

compile 'com.android.volley:volley:1.1.0'

The List<Anotacoes> listaHome is empty, ie no object is inserted.

One note: When I try to insert manually WITHIN% of%, it is NOT INSERTED. This way:

try {
       JSONArray postagens = response.getJSONArray("anotacao");
       for (int i = 0; i < postagens.length(); i++){
          JSONObject post = postagens.getJSONObject(i);

          // aqui ele deveria inserir, mas não insere
          listaHome.add(new Anotacoes("1","Teste 1", "texto de teste 1"));

          // aqui ele mostra o título no toast (2 registros)
          Toast.makeText(getApplicationContext(), post.getString("titulo"), Toast.LENGTH_SHORT).show();
       }
     }

Why does this occur? How can I create objects and insert them into a list of variables that are retrieved through the API?

    
asked by anonymous 15.03.2018 / 21:38

1 answer

1

You want to populate your list and then make use of it. However, filling and use should occur one after the other. I believe in your code this is not happening.

First you are declaring the list:

listaHome = new ArrayList<>();

Then declare your HTTP request:

JsonObjectRequest objectRequest = new JsonObjectRequest( ... );
request.add(objectRequest);

and probably then try to use the list (this part is not in the code that you included in the question, but I'm guessing).

Keep in mind that the HTTP request is being declared over and will not be executed immediately. So if you want to use the list then this statement will have to wait for it to run and at the end of that run make use of the list, and do not try to use immediately after the declaration.

In other words, the correct one would be to move the code that makes use of the list to soon after the block for that is filling the list.

    
15.03.2018 / 23:09