我有一个andoird应用程序,我希望将结果返回到我的android TextView 。我在浏览器上测试了它,是的,结果显示了,但它没有返回任何内容到我的 Android 应用程序。
以下代码是我的 php 文件。
<?php
$accounts = mysql_connect("localhost","user123","1234")
or die (mysql_error());
mysql_select_db("user123",$accounts);
if(isset($_GET["id"])){
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM table123 WHERE id = $id");
if($result != null){
$result = mysql_fetch_array($result);
$user = array();
$user["username"] = $result["username"];
$user["password"] = $result["password"];
echo json_encode($user);
}else{
}
}else{
$user["username"] = "not found";
$user["password"] = null;
echo json_encode($user);
}
?>
以下代码是浏览器结果
{"username":"not found","password":null}
即使结果没有找到,它也应该返回 $user show me "not find"。不可能没有任何内容返回到流中。我已经在我的浏览器上测试了它,是的,它返回了 $user 给我。我的代码有什么问题吗?
最佳答案
将“localhost”
替换为“10.0.2.2”
并将此行添加到您的 list 中:
<uses-permission android:name="android.permission.INTERNET" />
如果您使用 Android 3.0 或更高版本(在本例中您是这样做的)。
然后如果可能的话将模拟器更改为2.3.3,然后再次尝试运行代码。
或
将这些行添加到您的代码中:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
关于java - XAMPP 服务器未返回 Json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14974677/