我有关于 google volley 库的问题。我创建了一个 php 文件来检查以下状态
- 这是一个post请求
- 它将变成什么变量
这看起来像这些
if($_SERVER["REQUEST_METHOD"]=="POST"){
include 'connection.php';
showClassmates();
}
function showClassmates(){
global $connect;
if(isset($_POST['table'])){
$row = mysqli_real_escape_string($connect, strtolower($_POST['table']));
$query = " Select * FROM $row; ";
$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; charset=UTF-8');
echo json_encode(array("classmates"=>$temp_array));
mysqli_close($connect);
}
}
现在我想使用 Android 应用程序加载此列表。我为此创建了一个布局资源。之后我阅读了 volley 文档,我认为从 mysql 表加载 json 数据是个好主意。但我不知道如何在一个请求中生成表名并加载 header 字符集列表。有人有想法吗?感谢您的帮助:D
哦,这是我老师的作业项目,我想他讨厌我 xD 但重要的是,它会被评分:(
最佳答案
我使用此代码来获取 json 数据,但如何解决 post[table] 的问题?
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.POST,
showUrl, new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray products = response.getJSONArray("products");
for (int i = 0; i < classmates.length(); i++) {
JSONObject classmatelist = classmates.getJSONObject(i);
String classmate = product.getString("classmate");
items.add(classmates);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), R.string.error_toast, >Toast.LENGTH_LONG).show();
}
});
requestQueue.add(jsonObjectRequest);
关于php - Android Volley 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46564670/