doufei6456 2015-07-08 01:41
浏览 48

尝试使用Httppost连接到本地数据库

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!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 公交车和无人机协同运输
    • ¥15 stm32代码移植没反应
    • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
    • ¥100 连续两帧图像高速减法
    • ¥15 组策略中的计算机配置策略无法下发
    • ¥15 如何绘制动力学系统的相图
    • ¥15 对接wps接口实现获取元数据
    • ¥20 给自己本科IT专业毕业的妹m找个实习工作
    • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
    • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)