0

我有一个 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 found"。不可能没有任何东西返回到流中。我已经在我的浏览器上对其进行了测试,是的,它返回了 $user 给我。我的代码有什么问题吗?

4

2 回答 2

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);
于 2013-02-20T07:56:02.120 回答
1

我的 PHP 源代码没有任何问题。替换以下代码后我得到了它

HttpClient httpClient = new DefaultHttpClient();

DefaultHttpClient httpClient = new DefaultHttpClient();

一切正常。

于 2013-02-20T13:08:14.400 回答