im am trying to get user data from my Date base hosted with wamp. I sent a post request to an php file which return a json object but the app gets stuck in HttpResponse response = cliente.execute(request);
This is my code:
private void obtenerJson() {
EditText nombre = (EditText) findViewById(R.id.txtnombre);
EditText password = (EditText) findViewById(R.id.txtpassword);
String nombreIngresado = nombre.getText().toString().trim();
String passwordIngresado = password.getText().toString().trim();
List<NameValuePair> datos = new ArrayList<NameValuePair>();
datos.add(new BasicNameValuePair("nombre", nombreIngresado));
datos.add(new BasicNameValuePair("password", passwordIngresado));
HttpClient cliente = new DefaultHttpClient();
HttpPost request = new HttpPost("http://10.0.2.2/myfiles/myrequest.php");
BufferedReader lector =null;
StringBuffer stringBuffer = new StringBuffer("");
try {
request.setEntity(new UrlEncodedFormEntity(datos));
HttpResponse response = cliente.execute(request);
HttpEntity entidad = response.getEntity();
if (entidad != null) {
String resultado = EntityUtils.toString(entidad, "utf-8");
}
lector = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String linea = "";
String separadora = System.getProperty("line.separator");
while ((linea = lector.readLine()) != null){
stringBuffer.append(linea + separadora);
}
lector.close();
interpretarJSon(stringBuffer.toString(), nombreIngresado, passwordIngresado);
} catch (Exception e) {
}
}
The code below HttpResponse response = cliente.execute(request); doesn't matter. The thing is that when I send the request, it return nothing. I use a Log.d to show the response content, but It has nothing to show.
This is my php code:
<?php include('funciones.php');
$nombre = $_POST["nombre"];
$password = $_POST["pass"];
$result = getSQLResultSet("SELECT nombre, password
FROM `usuario` where nombre=".$nombre."");
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
foreach ($row as $var => $comparar){
if($nombre == $var ){
$nombreok = true;
}
if($pass == $var){
$passok = true;
}
if($nombreok && $passok){
$devolver[] = $row;
}
}
}
echo json_encode($devolver);
?>
This is the code from funciones.php:
<?php
function getSQLResultSet($commando){
$mysqli = new mysqli("localhost", "root", "", "basededatos");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s
", $mysqli->connect_error);
exit();
}
$var = $mysqli->execute($commando);
//return $mysqli->store_result();
$mysqli->close();
return $var;
}
?>
I think I might be using a wrong ip, or the problem is in my php code. I will translate the code if necessary. Thank you!