Android VOLLEY send json and receive json in response

2

Good, what I'm trying to do is send a json from Android with Volley to a php that I have on my server, within the php I make some queries and I need to return another json (response) to work with that data. I clarify that PHP works well with another APP that uses httpPost, but since it is deprecated I am updating the APP using VOLLEY

The problem that I have is that it does not enter the code, with the debug I realize that of the first line

JsonObjectRequest postRequest = new JsonObjectRequest( Request.Method.POST, URL,
                        jsonParams,
                        new Response.Listener<JSONObject>() {

Jump directly to the next line out of this function maybe I have something wrong with the parameters or the declaration, I leave the code

**** Desde esta primera linea salta directamente a requestQueue.add(postRequest); sin entrar *****


JsonObjectRequest postRequest = new JsonObjectRequest( Request.Method.POST, URL,
    jsonParams,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONObject Obj = response;
                JSONArray jarray = Obj.getJSONArray("usu");
                if (jarray.length() <= 0) {
                    Toast.makeText(getApplicationContext(), "SIN REGISTROS PARA ACTUALIZAR", Toast.LENGTH_SHORT).show();
                } else {
                    for (int i = 0; i < jarray.length(); i++) {
                        JSONObject object1 = jarray.getJSONObject(i);

                        String variable = object1.getString("variable");
                        String variable2 = object1.getString("variable2");
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            dialog.dismiss();
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
            dialog.dismiss();
        }
    }) {

    };
    *** salta directamente a esta línea ****
    requestQueue = Volley.newRequestQueue(SettingsActivity.this);
    requestQueue.add(postRequest); 
    **** En la línea anterior me muestra lo siguiente debugeando: RequestQueue: [email protected] postRequest: "[] www.url.com/php/ejemplo.php 0xb7f7dc44 NORMAL null"

    }else{
        Toast toast1 = Toast.makeText(getApplicationContext(),"El Servicior y/o el Usuario no pueden estar vacíos", Toast.LENGTH_SHORT);
        toast1.show();
    }

    }
});
}

THANK YOU !!!

    
asked by desarrollosTELLO 11.07.2017 в 18:36
source

2 answers

3

Well guys I finally solved it, do not ask because or how, are those mysteries of the computer hahaha, I think I have not modified anything and just walked out and it works, I leave in case someone is interested. What this button does is to take the user and the server that are in inputs of the activity, send them as json parameters to a php. That php makes a selection of a table according to the user and it also returns a json that I take it, process it and show it in a TOAST as an example to verify if they arrived. What you see there of MCrypt () is to encrypt the data, GREETINGS

I'll leave the code for you if it's useful

btnGuardar.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // por si el usuario los cambio los vuelvo a referenciar
                requestQueue = Volley.newRequestQueue(SettingsActivity.this);
                usuario_celu = et_usuario_celu.getText().toString();
                servidor_celu = et_servidor_celu.getText().toString();
                if (!(servidor_celu == null || servidor_celu.equals("") || usuario_celu == null || usuario_celu.equals(""))){
                    mcrypt = new MCrypt();
                    try {
                        usuario_celu_e = MCrypt.bytesToHex(mcrypt.encrypt( usuario_celu));
                        servidor_celu_e = MCrypt.bytesToHex(mcrypt.encrypt( servidor_celu));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    String URL = servidor_celu.trim() + Globales.CREAR_RELACION;
                    dialog = new ProgressDialog(SettingsActivity.this);
                    dialog.setMessage("Registrando Usuario...");
                    dialog.show();


                    JSONObject jsonParams = new JSONObject();
                    try {
                        jsonParams.put("username", usuario_celu_e);
                        jsonParams.put("server", servidor_celu_e);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    JsonObjectRequest postRequest = new JsonObjectRequest( Request.Method.POST, URL,
                            jsonParams,
                            new Response.Listener<JSONObject>() {
                                @Override
                                public void onResponse(JSONObject response) {
                                    try {
                                        JSONObject Obj = response;
                                        JSONArray jarray = Obj.getJSONArray("usu");
                                        if (jarray.length() <= 0) {
                                            Toast.makeText(getApplicationContext(), "SIN REGISTROS PARA ACTUALIZAR", Toast.LENGTH_SHORT).show();
                                        } else {
                                            for (int i = 0; i < jarray.length(); i++) {
                                                JSONObject object1 = jarray.getJSONObject(i);

                                                String empresa_e = object1.getString("empresa");
                                                String sucursal_e = object1.getString("sucursal");
                                                String passw_e = object1.getString("password");
                                                String servidor_e = object1.getString("servidor");

                                                mcrypt = new MCrypt();
                                                try {
                                                    String empresa =  new String(mcrypt.decrypt(empresa_e));
                                                    String sucursal = new String(mcrypt.decrypt(sucursal_e));
                                                    String passw =    new String(mcrypt.decrypt(passw_e));
                                                    String servidor = new String(mcrypt.decrypt(servidor_e));
                                                    Toast toast2 = Toast.makeText(getApplicationContext(),"DATOS: " + empresa + " | " + sucursal + " | " + passw + " | " + servidor , Toast.LENGTH_SHORT);
                                                    toast2.show();
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }

                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                    dialog.dismiss();

                                }
                            },
                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    error.printStackTrace();
                                    dialog.dismiss();
                                }
                            }) {

                    };

                    requestQueue.add(postRequest);
                }else{
                    Toast toast1 = Toast.makeText(getApplicationContext(),"El Servicior y/o el Usuario no pueden estar vacíos", Toast.LENGTH_SHORT);
                    toast1.show();
                }

            }
        });
    }
    
answered by 12.07.2017 в 10:19
-1

It makes sense to skip all the code in the second block since the call is not made immediately but when volley receives the answer.

At the moment you call requestQueue.add(postRequest) volley queue that request and execute it asynchronously, therefore all the onResponse code will be executed after that call, when volley sends the request and receives the result.

In order to debug I suggest you put a breakpoint in the first line of onResponse and onErrorResponse that way it will stop the thread when you receive the answer of the call.

    
answered by 11.07.2017 в 19:01