php - jsonObjectRequest 错误消息为空

标签 php android mysql xampp

所以我正在开发一个 Android 应用程序,它从 xampp 服务器上的 MySQL 数据库检索数据。这是来自 android main 的代码,

Button scanButton = (Button) findViewById(R.id.scanButton);
String findUrl = "http://myIPaddress/webservice/findItem.php";
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
scanButton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){

        JsonObjectRequest jsonObjectrequest = new JsonObjectRequest(Request.Method.POST, 
        findUrl, new Response.Listener<JSONObject>(){
            @Override
            public void onResponse(JSONObject response){
                try{
                    //retrieve the items table from the database
                    JSONArray items = response.getJSONArray("items");
                    int i = 2;
                    //retrieve the 3rd row in the table
                    JSONObject item = items.getJSONObject(i);
                    String name = item.getString("name");
                    //send a LogCat message containing the name of the row                  
                    Log.d("name",name); 
                } catch(JSONException e){
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError error){
                //in case of an error, send a logcat message containing the error
                Log.d("error","" + error.getMessage());
            }
        });
        requestQueue.add(jsonObjectrequest);
    }
});

}

php 脚本位于 webservice 文件夹中,该文件夹位于 xampp 文件夹的 htdocs 文件夹中。这是 connection.php 的脚本(声明并设置数据库连接),

<?php

define('hostname', 'localhost');
define('user','root');
define('password','');
define('databaseName','webservice');

$connect = mysqli_connect(hostname, user, password, databaseName);

?>

以及 findItem.php 的脚本(从本地主机 phpmyadmin 中的“webservice”数据库返回“items”表),

<?php

if($_SERVER["REQUEST_METHOD"]=="POST"){
    include 'connection.php';
    showItem();
}

function showItem(){
    global $connect;

    $query = "Select * FROM ITEMS";

    $result = mysqli_query($connect, $query);
    $number_of_rows = mysqli_num_rows($result);

    $temp_array = array();

    if($number_of_rows > 0){
        while($row = mysqli_fetch_assoc($result)){
            $temp_array[] = $row;
        }
    }
    header('Content-Type: application/json');
    echo json_encode(array("items"=>$temp_array));
    mysqli_close($connect);
}

?>

我遇到的错误发生在运行时。当我单击 Android 模拟器中的按钮时,logcat 消息不是“items”表第三行中的名称。有错误消息,但错误最终为空。我的问题基本上是可能是什么原因造成的?我已确保 php 脚本位于正确的位置(xampp/htdocs/webservice),IP 地址正确,apache 和 mysql 的 xampp 服务器已打开,在 android list 文件中写入了正确的权限,并且名为“webservice”的数据库中存在至少包含 3 行的“items”表。欢迎任何反馈,提前谢谢您。

最佳答案

因此,尽管我没有收到有关此问题的任何答案,但我能够找出问题所在。首先,我使用 postman 应用程序(google chrome 扩展)检查数据库是否存在,因为我在 php 脚本中使用了 post 方法。该数据库确实存在。因此,问题很可能是 IP 地址,尽管我在问题中的假设不正确。我原来在 findUrl 字符串中的 IP 地址是正确的,但仍然是错误的。这是一个有效的 IP 地址,但数据库位于本地托管服务器上;因此,该IP地址应该是本地IP地址。由于某种原因,我使用的 IP 地址不是本地 IP 地址,所以这就是修复方法。如果有人想知道我如何在 Windows 上找到本地 IP 地址,只需运行命令提示符并输入“ipconfig”即可。本地 IP 地址应为“ipv4 地址”。

关于php - jsonObjectRequest 错误消息为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33991414/

相关文章:

php - PHP中变量扩展与sprintf的性能

php - MySQL PDO dbname 在 MySQL 升级后停止工作

java - 如何将 View 放在确切的列和行中并使其填充该列 GridLayout android

java - 将FBO纹理与后处理后的主图像相结合以实现发光效果

php - 我怎样才能使这个数据库模式更好?

mysql - #1064 SQL 语法错误

php - 如何使用laravel中的with()函数打印表关系中的数据

php - AFNetworking 将 UIImage 上传为 NSData -> PHP 脚本移动文件

php - 排行榜中显示的数据缓慢

android - XMPP - 资源绑定(bind)有用性