jsonObjectRequest 错误消息为空
jsonObjectRequest error message is null
所以我正在开发一个 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 的脚本(returns 来自 "webservice" 数据库的 "items" table 我的本地主机 phpmyadmin) ,
<?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" table 第 3 行的名称。有一条错误消息,但错误最终为空。我的问题基本上是什么可能导致这种情况?我已确保 php 脚本位于正确的位置(xampp/htdocs/webservice),IP 地址正确,apache 和 mysql 的 xampp 服务器已打开, android 清单文件中写入了正确的权限,并且在名为 "webservice" 的数据库中存在至少 3 行的 "items" table。感谢任何反馈,提前致谢。
所以虽然我没有收到关于这个问题的任何答案,但我能够找出问题所在。首先,我使用 postman 应用程序(google chrome 扩展名)检查了数据库是否存在,因为我在 php 脚本中使用了 post 方法。该数据库确实存在。因此,尽管我在问题中的假设不正确,但问题很可能是 ip 地址。我原来在 findUrl 字符串中的 ip 地址是正确的,但仍然是错误的。这是一个有效的 IP 地址,但数据库位于本地托管的服务器上;因此,IP 地址应该是本地 IP 地址。出于某种原因,我使用的 ip 地址不是本地的,所以这就是修复。如果有人想知道我是如何在 Windows 上找到本地 IP 地址的,只需 运行 命令提示符并键入 "ipconfig"。本地 ip 地址应为 "ipv4 address"。
所以我正在开发一个 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 的脚本(returns 来自 "webservice" 数据库的 "items" table 我的本地主机 phpmyadmin) ,
<?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" table 第 3 行的名称。有一条错误消息,但错误最终为空。我的问题基本上是什么可能导致这种情况?我已确保 php 脚本位于正确的位置(xampp/htdocs/webservice),IP 地址正确,apache 和 mysql 的 xampp 服务器已打开, android 清单文件中写入了正确的权限,并且在名为 "webservice" 的数据库中存在至少 3 行的 "items" table。感谢任何反馈,提前致谢。
所以虽然我没有收到关于这个问题的任何答案,但我能够找出问题所在。首先,我使用 postman 应用程序(google chrome 扩展名)检查了数据库是否存在,因为我在 php 脚本中使用了 post 方法。该数据库确实存在。因此,尽管我在问题中的假设不正确,但问题很可能是 ip 地址。我原来在 findUrl 字符串中的 ip 地址是正确的,但仍然是错误的。这是一个有效的 IP 地址,但数据库位于本地托管的服务器上;因此,IP 地址应该是本地 IP 地址。出于某种原因,我使用的 ip 地址不是本地的,所以这就是修复。如果有人想知道我是如何在 Windows 上找到本地 IP 地址的,只需 运行 命令提示符并键入 "ipconfig"。本地 ip 地址应为 "ipv4 address"。