Skip to content
Advertisement

Volley jsonObjectRequest with parameter not working

Hello i am trying to send a parameter with my volley request and then get a json file to use with my RecyclerView. I have tested this without parameters and i am getting the expected result but with the parameter i do not get anything back.

private void parseJSON() {
        String url = "http://10.0.2.2/getThesis.php";  //URL OF THE PHP FILE USED

        Map<String, String> params = new HashMap();
        params.put("user", email);

        JSONObject parameters = new JSONObject(params);

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, parameters,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("assigned_thesis"); //THE NAME OF THE JSON FILE

                            for(int i = 0; i < jsonArray.length(); i++) {
                                JSONObject thesis = jsonArray.getJSONObject(i);

                                String title = thesis.getString("thesis_title");    //THE NAME OF THE COLUMN IN THE JSON FILE
                                String fullName = thesis.getString("full_name");

                                mExampleList.add(new ExampleItem(title, fullName));
                            }

                            mExampleAdapter = new ExampleAdapter(PendingChoiceThesis.this, mExampleList);   //USAGE OF THE CUSTOM ADAPTER, CHANGE CONTEXT TO BE THE SAME AS THE JAVA CLASS
                            mRecyclerView.setAdapter(mExampleAdapter);
                            mExampleAdapter.setOnItemClickListener(PendingChoiceThesis.this);   //CHANGE CONTEXT TO BE THE SAME AS THE JAVA CLASS
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        mRequestQueue.add(request);
    }

What i am trying to figure out is if my code for the volley request has an error, or if the error is elsewhere in my project.

(for reference, this is where i want to make use of the paramater in my php file)

$email = $_POST['user'];
$dep_query = mysqli_query($con, "SELECT department_ID FROM user_accounts NATURAL JOIN users WHERE email = '$email'");
$result = mysqli_fetch_assoc($dep_query);

Advertisement

Answer

Fixed my problem by adding this under the errorResponse

 @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json; charset=utf-8");
            return headers;
        }

And also changing my php to this

$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON ); 
$var = $input->user; //user is from the parameters jsonobject
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement