如何根据当前登录用户显示所有数据

时间:2018-09-20 00:53:05

标签: php android json android-volley

userSection.php

<?php  
require_once '../include/DbOperations.php';

$response = array(); 
if($_SERVER['REQUEST_METHOD']=='GET'){
    $uname = trim($_GET['username']);
    if(isset($uname)){
       $db = new DbOperations(); 

    if($db->studentLogin($uname)){
        $sections = $db->studentSection($uname);
        $response['error'] = false;         
        $response['id'] = $sections['id'];
        $response['lastname'] = $sections['lastname'];
        $response['section'] = $sections['section'];
        $response['year_level'] = $sections['year_level'];
        $response['school_year'] = $sections['school_year'];

    }else{
        $response['error'] = true; 
        $response['message'] = "No Data";          
    }

}else{
    $response['error'] = true; 
    $response['message'] = "Required fields are missing";
}
}
echo json_encode($response);

sectionList.class

StringRequest stringRequest = new StringRequest(Request.Method.GET, Constants.USER_GRADE,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                  //it should display the data of current user id
                  //On this part it doesn't display anything.
                  //example `id`=`1`. it should display all `id` equals to `1`

                            //converting the string to json array object
                            JSONArray array = new JSONArray(response);
                            if(sectionList!=null) {
                                sectionList.clear();
                            }
                                //traversing through all the object
                                for (int i = 0; i < array.length(); i++) {

                                //getting product object from json array
                                JSONObject sections = array.getJSONObject(i);

                                if (!sections.getBoolean("error")) {
                                        //adding the product to product list
                                        sectionList.add(new ListGradeData(
                                                sections.getInt("id"),
                                                sections.getString("section"),
                                                sections.getString("level"),
                                                sections.getString("schoolyear")
                                        ));
                                }
                            }

                        //creating adapter object and setting it to recyclerview
                        LatestGradeAdapter adapter = new LatestGradeAdapter(getActivity(), sectionList);
                        recyclerView.setAdapter(adapter);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    //adding our stringrequest to queue
    Volley.newRequestQueue(getActivity().getApplicationContext()).add(stringRequest);

如何显示id = 1的所有数据。 我当前的用户名id1。但是,每次我运行该应用程序时,它不会显示任何空白。 我也试着把它放

{
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("username", username);
                return params;
            }

        };

但仍然对我不起作用。

1 个答案:

答案 0 :(得分:1)

截击中的 GET 请求参数作为 URL 的一部分传递,因此将URL更改为

String url = "http://youraddress.com/userSection.php?username=" + username;

通过getParams()方法传递参数是在 POST 请求中完成的。