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?