google / volley

https://google.github.io/volley
Apache License 2.0
3.38k stars 754 forks source link

Por que Volley retorna null #258

Closed alosada1960 closed 5 years ago

alosada1960 commented 5 years ago

Soy un estudiante autodidacta de la programación en Android con algunos conocimientos básicos de programación en Java, PHP y SQL. En mis ejercicios prácticos cree esta pequeña aplicación para probar los conocimientos que voy adquiriendo y se me ha presentado el siguiente problema:

Esta aplicación funcionaba y funciona con las versiones de Android anteriores a SDK 26 pero una vez que cambio a las últimas versiones 26, 27 y 28 la respuesta de Volley es null, estoy utilizando Wamp como servidor local y la consulta a la DB o ejecución del "programa.php" en el navegador funciona perfectamente. Realmente entiendo que no tengo todos los conocimientos para lograr darme cuenta de qué falta o donde esta el error. Por favor necesito de alguna persona que desinteresadamente ayude a este mortal que lo que mas desea es aprender. Gracias

A continuación les muestro el código:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

TextView textView;
EditText editText,editText2;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = findViewById(R.id.textView);

    editText = findViewById(R.id.editText);
    editText2 = findViewById(R.id.editText2);
    button = findViewById(R.id.button);
    button.setOnClickListener(this);

}

@Override
public void onClick(View v) {

    String usu = editText.getText().toString();
    String pass = editText2.getText().toString();
    if (usu.length() == 0)
        Toast.makeText(getApplicationContext(), "El campo Nombre no puede estar en blanco", Toast.LENGTH_LONG).show();

    if (v.getId() == R.id.button){

        final String url = "http: ...";

       JsonArrayRequest req = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                if (response.length() == 0) {
                    Toast.makeText(getApplicationContext(), "El usuario no existe en el Sistema", Toast.LENGTH_LONG).show();
                }else {
                    try {

                        for (int i = 0; i < response.length(); i++) {

                            JSONObject id = (JSONObject) response.get(i);
                            JSONObject dato = id.getJSONObject("id");
                            System.out.println("dato mainactivity = " + dato);

                            int condicion = dato.getInt("2");

                            switch (condicion) {
                                case 1: {
                                    Intent intent1 = new Intent(getApplicationContext(), Administrar.class);
                                    startActivity(intent1);
                                    break;
                                }
                                case 0: {
                                    Intent intent2 = new Intent(getApplicationContext(), Ventas.class);
                                    startActivity(intent2);
                                    break;
                                }
                                default:
                                    break;
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), "ERROR : " + e.getMessage(), Toast.LENGTH_LONG).show();
                    }

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                NetworkResponse statusCode = error.networkResponse;
                System.out.println("Codigo " + statusCode);

            }
        });
        RequestQueue cola = Volley.newRequestQueue(getApplicationContext());
        cola.add(req);

    }

}

}

jpd236 commented 5 years ago

(I used Google Translate to translate from Spanish to English - I'm sorry if I've incorrectly interpreted anything that you mentioned, or for any difficulties in interpreting my response, as I'm not a native Spanish speaker).

Since this is a bug tracker for Volley rather than a general help forum, I think you will get more helpful responses if you try a more general help forum like StackOverflow or our usergroup, volley-users@googlegroups.com.

At a glance - given that your app is working on different versions of Android, I'd probably suggest that you monitor "adb logcat" output from the device while it's making the request to see if any errors are logged. There are some changes in API 26 (https://developer.android.com/about/versions/oreo/android-8.0-changes#networking-all) that might be impacting your test.