繁体   English   中英

带有参数的 Volley jsonObjectRequest 不起作用

[英]Volley jsonObjectRequest with parameter not working

您好,我正在尝试使用我的 volley 请求发送一个参数,然后获取一个 json 文件以与我的 RecyclerView 一起使用。 我已经在没有参数的情况下对此进行了测试,并且得到了预期的结果,但是使用参数我没有得到任何回报。

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);
    }

我想弄清楚的是我的凌空请求代码是否有错误,或者该错误是否在我的项目中的其他地方。

(作为参考,这是我想在我的 php 文件中使用参数的地方)

$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);

通过在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;
        }

并将我的 php 更改为此

$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON ); 
$var = $input->user; //user is from the parameters jsonobject

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM