php - Android Volley 库

标签 php android mysql json android-volley

我有关于 google volley 库的问题。我创建了一个 php 文件来检查以下状态

  1. 这是一个post请求
  2. 它将变成什么变量

这看起来像这些

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/

相关文章:

php - 当 HTML 在 1 行时,如何对 HTML 使用 preg_match_all 正则表达式?

Php 在特定字符处修剪字符串

php - 将冗余的 MySQL 结果解析为结构化数组

jquery - 底部 Phonegap 页面上的白色间隙

android.os.Parcelable[] 无法转换为...错误

php - 使用php将mysql数据库与postgres数据库集成

php - 如何在windows系统上为php7安装mysql驱动

php - 如何在删除时编写此代码的快捷方式

android - 如何检查横向模式下设备的方向是顺时针还是逆时针?

java - Spring启动JDBC异常