I am currently loading all the data in the table in a ReciclerView and everything goes OK. Now I want to do a search by name I modified the .php, leaving it as follows:
// post para recoger el nombre
$nombre=$_POST['nombrecito'];
// intentar conectar al servidor con el usuario y contraseña anteriores
$mysqli = new mysqli($hostname,$username,$password, $database);
/*
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
*/
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
// realizar una consulta que selecciona todas los datos de la tabla_Animadores
$query_search = "select * from tabla_Animadores WHERE name='".$nombre."'";
// crear un array para almacenar el json
$json = array();
// lanzar la consulta
$query_exec = $mysqli->query($query_search);
if (!$query_exec) {
echo "Falló la consulta de la tabla tabla_Animadores: (" . $mysqli->errno . ") " . $mysqli->error;
}
else {
while($row = $query_exec->fetch_array(MYSQLI_ASSOC)){
// almacena cada fila de la consulta en el array $json
$json[]=$row;
}
}
// cerrar la conexión con la BD mysql
$mysqli->close();
// mostrar por pantalla y codificar el vector $json como JSON
echo json_encode($json);
? >
The code in Android Studio is as follows:
public class MuestraDatos extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
//Iniciación de variables
private final String JSON_URL = MiURL;
private JsonArrayRequest request;
private RequestQueue requestQueue;
private List<Animadores> listaPersonas;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_muestra_datos);
listaPersonas = new ArrayList<>();
recyclerView = findViewById(R.id.recyclerviewid);
//obtenemos datos remotos
jsonrequest();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Botón flotante
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_camera) {
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
//obtenemos datos remotos
private void jsonrequest() {
request = new JsonArrayRequest(JSON_URL, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject = null;
for (int i = 0; i < response.length(); i++) {
try {
jsonObject = response.getJSONObject(i);
Animadores animadores = new Animadores();
animadores.setName(jsonObject.getString("name"));
animadores.setDescription(jsonObject.getString("description"));
animadores.setRating(jsonObject.getString("Rating"));
animadores.setCategorie(jsonObject.getString("categorie"));
animadores.setNb_episode(jsonObject.getInt("episode"));
animadores.setStudio(jsonObject.getString("studio"));
animadores.setImage_url(jsonObject.getString("img"));
//Añadimos el animadores a listaPersonas
listaPersonas.add(animadores);
} catch (JSONException e) {
e.printStackTrace();
}
}
//Muestra en el ReciclerView la listaPersonas (listaPersonas = todos los objetos extraidos del json)
muestraEnReciclerView(listaPersonas);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
public Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
params.put("nombrecito", "Javi");
return params;
}
};
requestQueue = Volley.newRequestQueue(MuestraDatos.this);
requestQueue.add(request);
}
private void muestraEnReciclerView(List<Animadores> listaAnimadores) {
//Creamos un objeto de la clase RecyclerViewAdapter y le decimos carge la listaPersonas.
RecyclerViewAdapter myadapter = new RecyclerViewAdapter(this, listaAnimadores);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(myadapter);
}
}
The problem is that I do not load any data. I'm passing it as a name to find Javi while in the database, but it does not load anything. However, by not passing parameters to the .php for POST, IF it loads all the records in the database.