【问题标题】:Volley jsonObjectRequest with parameter not working带有参数的 Volley jsonObjectRequest 不起作用
【发布时间】:2021-06-21 03:39:23
【问题描述】:

您好,我正在尝试使用我的 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);

【问题讨论】:

    标签: java php android android-volley


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-06-09
      • 2023-03-27
      • 1970-01-01
      • 2013-11-19
      • 2017-06-09
      • 1970-01-01
      • 2013-06-22
      • 1970-01-01
      相关资源
      最近更新 更多