-1

I create a project for my work, and I need to filter by User the data store on MySQL and sent to a listview with this:

MainActivity

public class pendientes extends AppCompatActivity{

//tvuser is elegible: John, Mike, etc
TextView tvuser;

private static final String BASE_URL = "http://jjj.freeoda.com/json_user_fetch.php?user=";
private static final String USUARIO = tvuser.getText().toString()+"";
private static final String apiurl = BASE_URL + USUARIO;

But I receive the follow error message: Non-static field 'USUARIO' cannot be referenced from a static context

DB MySQL:

ID Person User
1 Juan Lopez jhon
2 Esteban Morales mike
3 Luis Miguel jhon
4 Juan Gabriel jhon

In example, if I choose John, my listview only populate with: Juan Lopez Luis Miguel Juan Gabriel

If I choose Mike the results: Esteban Morales

My PHP API

<?php
$conn=mysqli_connect("localhost","user","pass");
mysqli_select_db($conn,"dbname");
$user=$_GET['user'];
$qry="select * from jurado where user= '$user'";
$raw=mysqli_query($conn,$qry);
while($res=mysqli_fetch_array($raw))
{
     $data[]=$res;
}
print(json_encode($data));
?>

How could I do to send a variable data (USER) to URL?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Dec 27 '20 at 23:30

1 Answers1

1

You make at least 2 mistakes

TextView tvuser; is a non-static class field that does not exist until you create an instance of class pendientes

and also if tvuser were static you couldn't call .getText() on it since you would do it over a null reference. In java object variables are null until you assign them an object, usually using new.

from56
  • 3,976
  • 2
  • 13
  • 23
  • Hola Lluis, gracias por responder, soy un poco nuevo en esto, alguna idea de como solucionar el problema? muchas gracias – Carlos Kesqui Dec 27 '20 at 23:34
  • De poco servirá que aquí ponga unas líneas de código porqué siento decir que tu conocimiento de Java no es suficiente para llegar a programar una conexión con base de datos. Deberías mejorar tu nivel de Java – from56 Dec 28 '20 at 09:05
  • Hola Luis, te deje un video en youtube: https://youtu.be/26GpDMsZg-s , te cuento que es verdad, estoy programando hace 3 meses, desde 0, totalmente nuevo, así que me falta bastante, pero aun así considero que en tan poco tiempo, logré algo de lo que me enorgullezco, ya que tenia 0 conocimientos en programación, y necesito incentivos para lograrlo, tus respuestas, fueron motivadoras, muchas gracias! – Carlos Kesqui Dec 28 '20 at 15:28
  • Lo siento pero no tengo tiempo de ver ni responder a tu video, en ningún caso voy a ser tutor de tus proyectos. Que te vaya muy bien. – from56 Dec 28 '20 at 17:46
  • Gracias Lluis por responder nuevamente, comprendo que no tengas tiempo, en el video te explicaba que tenía conexión con la base de datos y que ese no era el problema, quizá no me exprese muy bien en ingles, pero solo necesito saber si existe forma de "transformar" un non-static hacia un static, asi mi BdD sabe puntualmente que necesito, la verdad no buscaba que seas mi tutor, ni lo busco en otra persona, solo queria un poco de ayuda con el código, Saludos, que te vaya muy bien también! – Carlos Kesqui Dec 28 '20 at 18:42