【问题标题】:why isn't Volley sending the variables in getParams()?为什么 Volley 不在 getParams() 中发送变量?
【发布时间】:2021-08-22 01:10:54
【问题描述】:

我正在尝试列出与应用程序用户相关的机器,方法是将用户名和密码发送到验证帐户的代码中提到的网页,然后发回我数据库中机器的信息。 PHP 运行良好,但用户名和密码未发送。

这里是请求的代码:

private void getServerResponse(String username,String password) throws IOException {
        String urlS = "http://10.0.2.2/send/sendmachines.php";
        RequestQueue RQ= Volley.newRequestQueue(this);
        JsonArrayRequest array_request = new JsonArrayRequest(Request.Method.POST, urlS, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                TextView proof =findViewById(R.id.proof);
                for(int i=0; i<response.length(); i++){
                    try {
                        proof.setText(response.getJSONObject(0).getString("machine_id"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                TextView proof =findViewById(R.id.proof);
                proof.setText(error.getMessage());

            }
        }){
            protected Map<String,String> getParams(){
                Map<String,String > params = new HashMap<>();
                params.put("username",username);
                params.put("password",password);
                return params;
            }
        };
        RQ.add(array_request);
    }

用户名和密码已用于登录并作为附加信息发送到此活动。

【问题讨论】:

  • 你怎么知道用户名和密码没有被发送?很确定这是您应该用来发送参数的方式...也许您的变量 usernamepassword 为空?
  • @AlbertoSinigaglia 我用 Toast 测试了它们。
  • 请使用调试器并“认真”检查发送到服务器的内容

标签: java php android mysql android-studio


【解决方案1】:

最后发现代码没有通过getParams(),所以我把它变成了一个字符串请求,并将响应字符串解析成一个jsonArray。 我在网上找不到太多关于它的信息,所以 IDK 为什么这一切都不起作用,我不能认为这是一个答案,所以它更像是一个更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多