Java 正在尝试使用Httppost连接到本地数据库
我正在尝试从我的数据库中获取用户数据,数据库由wamp托管。 我向一个php文件发送了一个post请求,该文件返回一个json对象,但应用程序在HttpResponse response=cliente.execute(请求)中卡住了 这是我的代码:Java 正在尝试使用Httppost连接到本地数据库,java,php,android,Java,Php,Android,我正在尝试从我的数据库中获取用户数据,数据库由wamp托管。 我向一个php文件发送了一个post请求,该文件返回一个json对象,但应用程序在HttpResponse response=cliente.execute(请求)中卡住了 这是我的代码: private void obtenerJson() { EditText nombre = (EditText) findViewById(R.id.txtnombre); EditText password = (EditTex
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) {
}
}
private void obtenerJson(){
EditText nombre=(EditText)findViewById(R.id.txtnombre);
EditText密码=(EditText)findViewById(R.id.txtpassword);
字符串nombreengresado=nombre.getText().toString().trim();
字符串PasswordIngreado=password.getText().toString().trim();
List datos=new ArrayList();
添加(新的BasicNameValuePair(“nombre”,NombreIngreasado));
添加(新的BasicNameValuePair(“密码”,passwordIngresado));
HttpClient客户端=新的默认HttpClient();
HttpPost请求=新建HttpPost(“http://10.0.2.2/myfiles/myrequest.php");
BufferedReader选择器=null;
StringBuffer StringBuffer=新的StringBuffer(“”);
试一试{
setEntity(新的UrlEncodedFormEntity(datos));
HttpResponse response=cliente.execute(请求);
HttpEntity entidad=response.getEntity();
if(entidad!=null){
字符串resultado=EntityUtils.toString(entidad,“utf-8”);
}
选择器=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
字符串linea=“”;
字符串separadora=System.getProperty(“line.separator”);
而((linea=lector.readLine())!=null){
stringBuffer.append(linea+separadora);
}
lector.close();
解释器JSON(stringBuffer.toString(),nombreangresado,passwordIngresado);
}捕获(例外e){
}
}
HttpResponse response=cliente.execute(request)下面的代码;没关系。
问题是,当我发送请求时,它不会返回任何内容。我使用Log.d来显示响应内容,但它没有显示任何内容
这是我的php代码:
<?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);
?>
这是funciones.php中的代码:
<?php
function getSQLResultSet($commando){
$mysqli = new mysqli("localhost", "root", "", "basededatos");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$var = $mysqli->execute($commando);
//return $mysqli->store_result();
$mysqli->close();
return $var;
}
?>
我想我可能使用了错误的ip,或者问题出在我的php代码中。
如果有必要,我会翻译代码。
谢谢大家! 不幸的是,要远程解决WAMP连接的问题几乎是不可能的。至于您的代码,看起来您可能正试图在主线程上执行网络操作。您需要以某种方式使用后台线程,通常是异步任务。还要注意的是,
DefaultHttpClient
不推荐用于Android 5.1(API 22)。看看我最近写的这篇博文,我想它会有所帮助:我忘了说我在一个扩展AsyncTask的类中运行它,我将检查DefaultHttpClient。好的,那就好了。在这种情况下,您的代码中可能没有任何错误,快速浏览一下就可以了。只要意识到它在安卓5.1上不起作用。很可能是连接到本地数据库时出现问题,不幸的是,远程调试非常困难。祝你好运尝试在android浏览器中打开ip以检查连接