Jsonobjectrequest does not enter onResponse()

Asked

Viewed 125 times

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 consult the values of Webservice in Json. But every time I call this method he doesn’t get inside the Onresponse(), it jumps right to the end, in the Queue.add(getRequest).

The URL is right and the tags are right.

 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);

}
  • The request first enters the queue (Queue.add) and only after your System is executed. Look at your log, which for sure you have the information there.

1 answer

1

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

However, at no time do you let EXPLICIT that getRequest has to run Response.Listener.. To make matters worse, you delegate the execution to a queue when you call the Queue.add(getRequest method);

To make it a crucible worse, you’re not running anything in line either...

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

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

It goes in Volley’s documentation....

  • Thank you for the reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.