JsonObjectRequest does not enter onResponse ()

1

I'm doing a home automation project with Android and Arduino and I have this snippet of code in my class whose purpose is to query the values of the WebService in Json. But every time I call this method it does not go into OnResponse () , it jumps right to the end, in queue.add (getRequest) .

The URL is right and the tags too.

 public void lerLuminosidade(Context contexto) {
    String IP = URL_LER;

    RequestQueue queue = Volley.newRequestQueue(contexto);

    JsonObjectRequest getRequest =
            new JsonObjectRequest(Request.Method.POST, IP, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    try {
                        if (jsonObject.has("luminosidadeSala")) {
                            luminosidade[0] = jsonObject.getString("luminosidadeSala");
                        }
                        if (jsonObject.has("luminosidadeQuarto")) {
                            luminosidade[1] = jsonObject.getString("luminosidadeQuarto");
                        }
                        if (jsonObject.has("luminosidadeGaragem")) {
                            luminosidade[2] = jsonObject.getString("luminosidadeGaragem");
                        }
                        if (jsonObject.has("luminosidadeCozinha")) {
                            luminosidade[3] = jsonObject.getString("luminosidadeCozinha");
                        }
                        if (jsonObject.has("fanSala")){
                            ventilador[0] = jsonObject.getString("fanSala");
                        }
                        if (jsonObject.has("portaoGaragem")) {
                            int portao = Integer.parseInt(jsonObject.getString("portaoGaragem"));
                            if (portao >= 1) {
                                statusPortao = true;
                                portaoGaragem[0] = "1";
                            }
                            else {
                                statusPortao = false;
                                portaoGaragem[0] = "0";
                            }
                            if (portao == 0) {
                                statusPortao = false;
                                portaoGaragem[0] = "0";
                            }
                        }

                        Log.d("Resposta: ", jsonObject.toString());

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.e("Error", "Não foi possível acessar: " + URL_LER);

                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Log.d("Error.Response", volleyError.getMessage());

                }
            });
    queue.add(getRequest);

}
    
asked by anonymous 26.11.2015 / 16:14

1 answer

0

Dude, First of all: this code is mega confusing. You are mounting an instance of the JsonObjectRequest object passing in the last parameter a listener - which is a class that has a behavior that is where you debug.

>

To get a better crucible, you're also not running anything in the queue ...

You have built a hierarchy of instantiated classes by passing them as parameters from other classes and did not give the initial execution command ....

This is not linear code, although the writing is ...

Go to the Volley documentation.

    
28.11.2015 / 13:41