How to pass value into GET method in Android Studio?

1.9k views Asked by At

In have these data in my database, and some of the users have same DEPT value. My goal is when user login into page (i.e A1) is loggedin . It only display all the user that have the same dept value only which is A1,B1,E1.

---------------------------
| id   | user_id | dept   |
---------------------------
| 1    | A1      | ITD    | 
| 2    | B1      | ITD    |
| 3    | C1      | FAD    |
| 4    | D1      | PNC    |
| 5    | E1      | ITD    |
| 6    | F1      | PSSB   |

But the problem is my user_id and dept output data is in JAVA Android Studio.

Plus, in this case request.GET is applied instead of POST.

JAVA File

public class Test extends AppCompatActivity{
    public static final String dept = "DEPT";
    public static final String user_id   = "ID";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        final String dept   = (String) extras.get("DEPT");
        final String user_id     = (String) extras.get("USERID");
        getData();
    }

    private void getData(){
        String url = "https://www.aaa.com/test.php";
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                JSONObject j = null;
                try{
                    j       = new JSONObject(response);
                    result  = j.getJSONArray(Config.JSON_ARRAY);
                    getStaffs(result);
                }catch (JSONException e){
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(Test.this);
        requestQueue.add(stringRequest);
    }
}

Is it possible or am i doing it the wrong way ?

And also my PHP file as below.

PHP file

<?php
    $connect      = mysqli_connect("localhost", "root", "root", "database");
    global $connect;   
    if(isset($_GET['dept']))
    {
        $dept = $_GET['dept'];
        $sql        = "SELECT * FROM table WHERE dept='$dept'";
        $get        = mysqli_query($connect, $sql);
        $result     = array();
            while($row  = mysqli_fetch_array($get)){
                array_push($result, array(
                        'user_id' => $row['user_id'],
                        'dept'     => $row['dept']
                    ));
            }
        echo json_encode(array('result' => $result));
        mysqli_close($connect);
    }
?>

Config.class

public class Config {
    public static final String JSON_ARRAY        = "result";
}

Thanks.

0

There are 0 answers