2013-02-20 37 views
0

我得到了一个andoird应用程序,我想结果返回到我的android textview。我在浏览器上测试它,结果显示,但它没有返回到我的Android应用程序。JSON对象不是从XAMPP服务器返回

以下代码是我的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返回给我。我的代码有问题吗?

+0

在本地主机上,请尝试将您的PC的IP地址代替。 – Bigflow 2013-02-20 07:48:33

+0

我正在使用android模拟器,我使用ip地址10.0.2.2。我不认为我的IP地址有任何问题,因为我在另一个项目上测试过 – edisonthk 2013-02-20 07:50:27

+0

我仍然会尝试使用本地主机。我知道使用本地主机而不是10.0.2.2出现了错误,并且不会发生任何logcat错误? – Bigflow 2013-02-20 07:52:28

回答

1

没有什么错我的PHP源代码。我得到了后,我将以下代码

HttpClient httpClient = new DefaultHttpClient(); 

DefaultHttpClient httpClient = new DefaultHttpClient(); 

而且一切工作就工作。

1

更换"localhost""10.0.2.2"

而且加入这行到你的清单:

<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); 
+0

我加了但还是一样的,doesn没有工作。 – edisonthk 2013-02-20 08:25:24

+0

@edisonTHK将权限行保留在您的文件中,您也必须拥有该权限,并且我更新了答案。这个答案也可以解决你昨天的问题。 – Bigflow 2013-02-20 08:34:05