将参数从 android 传递到 php 时出现问题

Problem passing parameters from android to php

我的 android 应用程序在使用 volley 发送参数时出现问题,我必须显示登录我的应用程序的用户的数据,数据在服务器中,在我的 php 我 return 它与 Json,当我在 php 中发送参数时它工作得很好,但在凌空 getParams 不发送我的全局值变量。

附加代码:

MainLista.java 这里我读了 Json

public class MainLista extends AppCompatActivity {

    TextView txt1;
    ListView listaPerfil;
    ArrayAdapter adapter;
    HttpURLConnection con;

    String LINK = "phpurl";
    String url = LINK + "?email=" + Globales.USER_EMAIL + "?pass=" + Globales.USER_PASS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_lista);
        listaPerfil = findViewById(R.id.listaPerfil);

        txt1 = findViewById(R.id.txt1);

        txt1.setText("email: " + Globales.USER_EMAIL + "\nPASS: " + Globales.USER_PASS);

        StringRequest sr = new StringRequest(Request.Method.POST, url, response -> { }, error -> {

            Toast.makeText(this, "ERROR", Toast.LENGTH_SHORT).show();

        }) {
            @Override
            protected Map<String, String> getParams() {

                Map<String, String> params = new Hashtable<>();
                params.put("email", Globales.USER_EMAIL);
                params.put("contrasena", Globales.USER_PASS);

                Log.d("IMPRESION", Globales.USER_EMAIL);
                Log.d("IMPRESION", Globales.USER_PASS);

                return params;
            }
        };

        RequestQueue rq = Volley.newRequestQueue(this);
        rq.add(sr);

        try {
            ConnectivityManager connMgr =(ConnectivityManager)
                    getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

            if (networkInfo != null && networkInfo.isConnected()) {

                new JsonTask().execute(new URL("phpurl"));

            } else {

                Toast.makeText(this, "ERROR DE CONEXION", Toast.LENGTH_LONG).show();

            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    private class JsonTask extends AsyncTask<URL, Void, List<Datos>> {

        @Override
        protected List<Datos> doInBackground(URL... urls) {
            List<Datos> datos = null;

            try {
                con = (HttpURLConnection)urls[0].openConnection();
                con.setConnectTimeout(15000);
                con.setReadTimeout(10000);

                int statusCode = con.getResponseCode();

                if (statusCode != 200) {
                    datos = new ArrayList<>();
                    datos.add(new Datos("error", null, null, null, null, null, null));
                } else {
                    InputStream in = new BufferedInputStream(con.getInputStream());

                    JsonParser parser = new JsonParser();

                    datos = parser.leerFlujoJson(in);
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                con.disconnect();
            }
            return datos;
        }

        @Override
        protected void onPostExecute(List<Datos> datos) {
            if (datos != null) {
                adapter = new AdaptadorDatos(MainLista.this, datos);
                listaPerfil.setAdapter(adapter);

            } else {
                Toast.makeText(getBaseContext(), "ERROR DE PARSING JSON", Toast.LENGTH_LONG).show();
            }
        }
    }
}

php:

<?
    session_start();
    include('Conexion2.php');

    if($_SERVER['REQUEST_METHOD'] == 'GET') {

    $email  = $_GET['email'];
    $pass   = $_GET['contrasena'];

    $sql="SELECT c.nombre, c.apellido, c.telefono1, c.email, c.fechanacimiento, c.contrasena, f.imagen
            FROM clientes AS c
            INNER JOIN fotos AS f ON c.clienteid = f.clienteid
            WHERE c.email = '$email' AND c.contrasena = '$pass'";

$pdo = pdo();
$query = $pdo -> prepare($sql);
$query -> execute(array($cons));
$res = $query -> fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($res);

echo $json;
    }
?>

从php获得Json:

[{"nombre":"Fernando","apellido":"Villarreal","telefono1":"8672554040","email":"ricardito@gmail.com","fechanacimiento":"2021-02-11","contrasena":"12345678","imagen":"https:**********/FP-aYRsz-891.png"}]

在您的 PHP 代码中,您使用 GET 方法,而在 Volley 中,您使用 POST 因此这个工作你有 2 选择改变你的 php 到:

<?
    session_start();
    include('Conexion2.php');



    $email  = $_POST['email'];
    $pass   = $_POST['contrasena'];

    $sql="SELECT c.nombre, c.apellido, c.telefono1, c.email, c.fechanacimiento, c.contrasena, f.imagen
            FROM clientes AS c
            INNER JOIN fotos AS f ON c.clienteid = f.clienteid
            WHERE c.email = '$email' AND c.contrasena = '$pass'";

$pdo = pdo();
$query = $pdo -> prepare($sql);
$query -> execute(array($cons));
$res = $query -> fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($res);

echo $json;
    
?>

或将 Volley 代码更改为:

 StringRequest sr = new StringRequest(Request.Method.GET, url, response -> { }, error -> {

        Toast.makeText(this, "ERROR", Toast.LENGTH_SHORT).show();

    
    });
    
}

而且您还需要将 Link 更改为:

 String LINK = "https://viavel.com.mx/pruebas/PHPRicardo/ejemplomostrar.php"; 
 String url = LINK + "?email=" + Globales.USER_EMAIL + "&pass=" +Globales.USER_PASS;

对于 GET 方法中的多个值,您必须在值之间添加“&”